Instructor: Craig Duckett. Lecture 02: Thursday, March 29 th, 2018 SQL Basics and SELECT, FROM, WHERE

Similar documents
Instructor: Craig Duckett. Lecture 03: Tuesday, April 3, 2018 SQL Sorting, Aggregates and Joining Tables

Instructor: Craig Duckett. Lecture 07: Tuesday, April 17 th, 2018 Conflicts and Isolation, MySQL Workbench

Instructor: Craig Duckett. Lecture 04: Thursday, April 5, Relationships

Assignment 1 DUE TONIGHT

SQL Data Definition Language: Create and Change the Database Ray Lockwood

Instructor: Craig Duckett. Lecture 14: Tuesday, May 15 th, 2018 Stored Procedures (SQL Server) and MySQL

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

INTRODUCTION TO DATABASE

Instructor: Craig Duckett. Lecture 11: Thursday, May 3 th, Set Operations, Subqueries, Views

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Lecture 7. Instructor: Craig Duckett OUTPUT

Lecture 1: Relational Databases

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Lesson 17 Transcript: Troubleshooting

Lab # 2. For today s lab:

how its done in about the five most common SQL implementations.

Full file at

Slide 1 CS 170 Java Programming 1

CSC 326H1F, Fall Programming Languages. What languages do you know? Instructor: Ali Juma. A survey of counted loops: FORTRAN

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Chapter 17. Fundamental Concepts Expressed in JavaScript

Informatics 1: Data & Analysis

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

Week 2: The Clojure Language. Background Basic structure A few of the most useful facilities. A modernized Lisp. An insider's opinion

Slide 1 Java Programming 1 Lecture 2D Java Mechanics Duration: 00:01:06 Advance mode: Auto

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Lesson 3 Transcript: Part 2 of 2 Tools & Scripting

Informatics 1: Data & Analysis

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

BIT 115: Introduction To Programming LECTURE 3. Instructor: Craig Duckett

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

Instructor: Craig Duckett SQL SERVER WALK-THROUGH LAB 13 Lecture 13: Thursday, May 10 th, 2018 Microsoft Azure and SQL SERVER INTRO

MITOCW watch?v=0jljzrnhwoi

Lesson 3 Transcript: Part 1 of 2 - Tools & Scripting

Lesson 13 Transcript: User-Defined Functions

SQL Workshop. Introduction Queries. Doug Shook

FRAC: Language Reference Manual

MITOCW watch?v=rvrkt-jxvko

Data Base Concepts. Course Guide 2

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

DATABASE MANAGEMENT SYSTEMS PREPARED BY: ENGR. MOBEEN NAZAR

Hello, and welcome to another episode of. Getting the Most Out of IBM U2. This is Kenny Brunel, and

Informatics 1: Data & Analysis

Getting Information from a Table

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

Speech 2 Part 2 Transcript: The role of DB2 in Web 2.0 and in the IOD World

LABORATORY. 16 Databases OBJECTIVE REFERENCES. Write simple SQL queries using the Simple SQL app.

NCSS: Databases and SQL

SQL. History. From Wikipedia, the free encyclopedia.

SQL for MySQL A Beginner s Tutorial

12B. Laboratory. Databases. Objective. References. Write simple SQL queries using the Simple SQL applet.

CISC 3140 (CIS 20.2) Design & Implementation of Software Application II

INF 315E Introduction to Databases School of Information Fall 2015

[PDF] Database Systems: Design, Implementation, And Management (Book Only)

Full file at Chapter 2: An Introduction to SQL

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Microprocessors & Assembly Language Lab 1 (Introduction to 8086 Programming)

Program Fundamentals

PROFESSOR: Well, yesterday we learned a bit about symbolic manipulation, and we wrote a rather stylized

The following content is provided under a Creative Commons license. Your support

SQL stands for Structured Query Language. SQL is the lingua franca

Most of the class will focus on if/else statements and the logical statements ("conditionals") that are used to build them. Then I'll go over a few

Copy Data From One Schema To Another In Sql Developer

CSCE 120: Learning To Code

Quiz 3; Tuesday, January 27; 5 minutes; 5 points [Solutions follow on next page]

CS390 Principles of Concurrency and Parallelism. Lecture Notes for Lecture #5 2/2/2012. Author: Jared Hall

Relational Database Management Systems for Epidemiologists: SQL Part I

Lesson 5 Transcript: Client Connectivity

An Introduction to Stored Procedures in MySQL 5 by Federico Leven6 Apr 2011

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

Create your first workbook

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

LECTURE 02 INTRODUCTION TO C++

Today s Experts. Mastering Dates Using SEQUEL 1. Technical Consultant. Technical Consultant

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

In today s video I'm going show you how you can set up your own online business using marketing and affiliate marketing.

MySQL Stored Procedures

Java Programming Constructs Java Programming 2 Lesson 1

PHP Programming With MySQL: The Web Technologies Series Ebooks Free

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

The following content is provided under a Creative Commons license. Your support

SQL DDL. Intro SQL CREATE TABLE ALTER TABLE Data types Service-based database in Visual Studio Database in PHPMyAdmin

Free Downloads Microsoft Access Developer's Guide To SQL Server

PHP & MySQL For Dummies, 4th Edition PDF

Programming the Database

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

The Stack, Free Store, and Global Namespace

Chapter 2 Basic Elements of C++

tablename ORDER BY column ASC tablename ORDER BY column DESC sortingorder, } The WHERE and ORDER BY clauses can be combined in one

SQL. The Basics Advanced Manipulation Constraints Authorization 1. 1

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

CSCI 1060U Programming Workshop

Microsoft SQL Server Week1

Appendix A: Syntax Diagrams

Transcription:

Instructor: Craig Duckett Lecture 02: Thursday, March 29 th, 2018 SQL Basics and SELECT, FROM, WHERE 1

Assignment 1 is due LECTURE 5, Tuesday, April 10 th, 2018 in StudentTracker by MIDNIGHT MID-TERM EXAM is LECTURE 10, Tuesday, May 1 st, 2018 Assignment 2 is due LECTURE 12, Tuesday, May 8 th, 2018 in StudentTracker by MIDNIGHT 2

3 x 150 Points (450 points Total) Assignment 1 (Stage 1): DUE LECTURE 5 Tuesday, April 10 th Assignment 2 (Stage 2): DUE LECTURE 12 Tuesday, May 8 th Assignment 3 (Stage 3): DUE LECTURE 20 Tuesday, June 5 th Database Presentation: DUE LECTURE 20 Tuesday, June 5 th 3

Tuesday (LECTURE 1) Database Design for Mere Mortals: Chapter 1 Thursday (LECTURE 2) The Language of SQL: Chapter 1: Relational Databases and SQL Chapter 2: Basic Data Retrieval 4

SQL Basics SQL Syntax SQL Keywords The SELECT Statement The FROM Clause The WHERE Clause BIT275: ICE 02 5

SQL Basics 6

SQL Basics SQL, or Structured Query Language, is the common language that lies at the heart of every Relational Database Management System* (the application that actually gets the SQL to run and do something) that you'll likely to use. Now, you've probably noticed the different pronunciation already. Some people say S-Q-L, some people say sequel. I tend to say S-Q-L in general and when talking about MySQL (the MySQL official documentation says that s how it should be pronounced), but I will say Microsoft Sequel Server (because that s how they pronounce it on-campus AT Microsoft) although will sometimes revert to S-Q-L Server if I ve been talking about SQL in general just before hand. In short, both ways are correct, but I would defer to what and how they are calling it where you work Now SQL, the language, has been around since the 1970s, and that's one of the few languages I was writing 20 years ago that I'm still writing now. A lot of other programming languages have come and gone, become fashionable and unfashionable, but SQL has stuck around, and I expect it to stay for a long time to come. But SQL is a very small, very focused language, and the key to first learning it is to understand that it's a little different from other programming languages you might have come across, like C, C++, Java, or C#. SQL is what's considered a declarative query language, as opposed to procedural or imperative languages. What it means is you use SQL to describe what you want, and you let the Database Management System (DBMS) handle how that's actually done. You don't have to manually lay out the algorithm, the different steps of the procedure as you would do in other programming languages.

*How Does SQL Run? A.java files is simply a text file that needs a compiler like jgrasp to run. An.html or.css or.js file are also all text files that need a browser application to run. A C or C++ or C# file are simply text files as well that also need a compiler to run. Like them, a.sql file is also a text file that needs a Database Management System (DBMS)* to run, although you can also enter your SQL queries directly into the DBMS without the need of a separate.sql file (as we shall see throughout the quarter, and starting with today s ICE). Different Types of Database Management Systems Access FileMaker Pro DB2 Sybase dbase InGres PostgreSQL Rocket U2 MySQL MariaDB SQL Server Oracle See: http://en.wikipedia.org/wiki/list_of_relational_database_management_systems

SQL Basics: How Does It Work? If I have a Books table with, perhaps, thousand different books in it, and I want to know which books have a list price of more than $40. Well, in a procedural or imperative language like C or Java, I'll have to write the steps to do this

SQL Basics I would probably write some (pseudo) code that would start at the first book and then would loop through all of them one by one. And every time going through this loop you're asking the question, "Is this more than $40? If so, do one thing. If not, do something else." Even writing this in pseudo code, I'd be writing a loop. I'd be writing conditions. I'd be writing returns.

SQL Basics With SQL, you don't describe all the steps, you just describe the outcome. You describe what you want the same way you might do in English, where you just say: or this written in SQL: That's it. Select or go get everything from the Books table where the price is more than $40. The Database Management System will take this, it will look at all your data, figure it out, and return the result set, whether that would be one book, 500 books, or even none, based on your query.

SQL Basics So SQL can be used to select, to retrieve, or read data, and to ask questions of it. Books Table

SQL Basics In fact, all of these things are referred to with the wonderful acronym of CRUD: Create, Read, Update, Delete. An SQL can be used to create not just your data, but to define the databases themselves. FYI: I will be discussing Data Manipulation Language (DML) and Data Definition Language (DDL) in Lecture 5

SQL Syntax http://www.w3schools.com/sql/sql_syntax.asp 14

SQL Syntax 1. Comment A comment is optional text that explains you program. Comments usually describe what a program does or why code was changed. Compilers are for humans the compiler ignores them. A comment is introduced by two consecutive hypens and continues until the end of the line. -- This is a comment Some DBMS (like Microsoft SQL Server) support the /*... */ (forward slash-asterisk character pairs) multiline comments (those who've taken Java or C# should recognize these). 2. SQL Statement An SQL statement is a valid combination of tokens introduced by a keyword. Tokens are the basic indivisible parts of the SQL language; they can t be reduced grammatically. Tokens include keywords, identifiers, operators, constants, and punctuation symbols. 3. Clauses An SQL statement has one or more clauses. In general, a clause is a fragment of an SQL statement introduced by a keyword, is required or optional, and must be given in a particular order. SELECT, FROM, WHERE, and ORDER BY are examples of clauses. 4. Keywords Keywords are words that SQL reserves because they have special meaning in the language. Using a keyword outside its specific context (as an identifier, for example)) causes an error. DBMSs use a mix of standard and nonstandard (proprietary) keywords. Search your DBMS documentation for keywords or reserved words. 5. Identifiers Identifiers are words that you (or the database designer) use to name database objects such as tables, columns, aliases, indexes, and views. 6. Terminating Semicolon AN SQL statement ends with a semicolon.

Authors Table 16

SQL Syntax SQL is a "freeform" language whose statements can: Be in uppercase or lowercase (SELECT, select, or SeLECt are considered to be identical keywords) Continue on the next line as long as you don't split words, tokens, or quoted strings in two. Be on the same line as other statements Start in any column Despite its flexibility, you should adopt a consistent style. I prefer to use uppercase keywords (e.g., SELECT) and an uppercase/lowercase mix for identifiers using Pascal case (e.g., FirstName). Although I don't do it myself, some SQL coders also like to indent each clause on its own line:

SQL Syntax Common Errors Omitting the terminating semicolon when using multiple statements (or Transact-SQL) Misspelling a keyword or identifier Mismatched or unmatched parentheses or quotes (e.g., double quotes instead of single quotes) Listing clauses out of order Not surrounding a string or datetime literal with single quotes Surrounding a numeric constant (literal) or the keyword NULL with quotes Mismatching a table and column name Creating a table with a bad name

SQL Keywords 19

SQL Keywords Structured Query Language is the shared vocabulary of relational databases. That's not for general purpose programming. It's a small language focused purely on working with databases and the first handful of keywords that you'll learn (maybe a dozen) will take care of 90% of everything you'd ever do in SQL.

The SELECT Statement See: http://www.w3schools.com/sql/sql_select.asp 22

The SELECT Statement In terms of frequency, there's one that's head and shoulders above all the others. By far the most common word you'll ever write in SQL is SELECT. This is the word we start with when we want to SELECT or read information from one of the tables in one of our databases. We're using it to ask the database a question, and we expect a reply. Actually, the better word is not question but query. A query as in the Q in SQL: Structured Query Language. if I want to SELECT some data out of my database, I'm going to have multiple tables and multiple columns in each table. So, I need to say what specific part of this database am I interested in selecting. The general format of a SELECT statement is the word SELECT, what columns you're interested in, then the word FROM what table? SELECT and FROM are your SQL keywords. They're part of the SQL language itself. Now, the columns and table names are up to you. It's whatever you named your columns and your tables in your database. See: http://www.w3schools.com/sql/sql_select.asp

The FROM Clause 24

SELECT Statement, FROM Clause

SELECT Statement, FROM Clause

SELECT Statement, FROM Clause

SELECT Statement, FROM Clause

The WHERE Clause 29

The WHERE Clause See: http://www.w3schools.com/sql/sql_where.asp

The WHERE Clause See: http://www.w3schools.com/sql/sql_where.asp

Uppercase, Lowercase, Semicolon?

Database Name with Dot Notation Dot notation allows you to prefix a table name to a column name to avoid ambiguity and to query multiple tables.

More About the WHERE Clause

More About the WHERE Clause

More About the WHERE Clause Single = sign <> NOT equal

More About the WHERE Clause AND OR See: http://www.w3schools.com/sql/sql_and_or.asp

More About the WHERE Clause IN (note the single quotes, and comma) See: http://www.w3schools.com/sql/sql_in.asp

More About the WHERE Clause % wildcard See: http://www.w3schools.com/sql/sql_wildcards.asp A substitute for 0 or more characters

More About the WHERE Clause _ wildcard A substitute for a single character See: http://www.w3schools.com/sql/sql_wildcards.asp

More About the WHERE Clause IS NULL See: http://www.w3schools.com/sql/sql_null_values.asp

More About the WHERE Clause IS NOT NULL See: http://www.w3schools.com/sql/sql_notnull.asp

BIT 275 ICE 02 43

BIT275: In-Class Exercises (ICE) SQL Query Exercise (solo, no partner ) Using what you've learned today about SQL, use the remainder of the class time to write SQL queries as part of the BIT275 ICE 02 exercise. Feel free to utilize whatever you might find useful in building these queries (a book, the PowerPoint slides, Google, etc). http://faculty.cascadia.edu/cduckett/bit275/lecture_02/ice02.html