Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

Similar documents
Chapter 3. Introduction to relational databases and MySQL. 2010, Mike Murach & Associates, Inc. Murach's PHP and MySQL, C3

Introduction to relational databases and MySQL

ISM 4212/4480 Milestone V Submission Sample

Chapter 3 Introduction to relational databases and MySQL

How to use SQL to work with a MySQL database

1) Introduction to SQL

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

Relational Database Management Systems for Epidemiologists: SQL Part I

More on MS Access queries

CSC Web Programming. Introduction to SQL

Appendix A. Using DML to Modify Data. Contents: Lesson 1: Adding Data to Tables A-3. Lesson 2: Modifying and Removing Data A-8

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

Lecture 04: SQL. Monday, April 2, 2007

Structured Query Language (SQL)

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

Full file at

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

SQL. Structured Query Language

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

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

SQL functions fit into two broad categories: Data definition language Data manipulation language

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

The Structured Query Language Get Started

Chapter-14 SQL COMMANDS

Solutions to the Problems in SQL Practice Problems by Sylvia Moestl Vasilik. John Weatherwax

Chapter 4: SQL Basics

DB2 SQL Class Outline

CST272 SQL Server, SQL and the SqlDataSource Page 1

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Northwind Database. Sample Output from TechWriter 2007 for Databases

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Lecture 04: SQL. Wednesday, October 4, 2006

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

Exact Numeric Data Types

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Microsoft Access Illustrated. Unit B: Building and Using Queries

Test Bank for Database Processing Fundamentals Design and Implementation 13th Edition by Kroenke

SQL. Char (30) can store ram, ramji007 or 80- b

1. Data Definition Language.

Lecture 06. Fall 2018 Borough of Manhattan Community College

You can write a command to retrieve specified columns and all rows from a table, as illustrated

Activant Solutions Inc. SQL 2005: Basic Data Manipulation

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Data Manipulation Language (DML)

2) SQL includes a data definition language, a data manipulation language, and SQL/Persistent stored modules. Answer: TRUE Diff: 2 Page Ref: 36

Jarek Szlichta

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Getting Information from a Table

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Definition of terms Objectives Interpret history and role of SQL Define a database using SQL data definition iti language Write single table queries u

Microsoft Office Illustrated Introductory, Building and Using Queries


MTA Database Administrator Fundamentals Course

How to use SQL to create a database

SQL DATA DEFINITION LANGUAGE

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

Queries. Chapter 6. In This Chapter. c SELECT Statement: Its Clauses and Functions. c Join Operator c Correlated Subqueries c Table Expressions

Course Topics. Microsoft SQL Server. Dr. Shohreh Ajoudanian. 01 Installing MSSQL Server Data types

Lesson 2. Data Manipulation Language

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

Lab # 4 Hands-On. DDL and DML Advance SQL Statements Institute of Computer Science, University of Tartu, Estonia

Tutorial 19 - Direct SQL programming

CMP-3440 Database Systems

SQL STRUCTURED QUERY LANGUAGE

Access Objects. Tables Queries Forms Reports Relationships

SQL BASICS WITH THE SMALLBANKDB STEFANO GRAZIOLI & MIKE MORRIS

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

618 Index. BIT data type, 108, 109 BIT_LENGTH, 595f BIT VARYING data type, 108 BLOB data type, 108 Boolean data type, 109

Greenplum SQL Class Outline

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

chapter 2 G ETTING I NFORMATION FROM A TABLE

Oracle Database 11g: SQL and PL/SQL Fundamentals

Databases II: Microsoft Access

Full file at

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

SQL Commands & Mongo DB New Syllabus

Structured Query Language (SQL) Part A. KSE 521 Topic 10 Mun Yi

Relational Database Management Systems for Epidemiologists: SQL Part II

Unit 1 - Chapter 4,5

11. Introduction to SQL

! Define terms. ! Interpret history and role of SQL. ! Write single table queries using SQL. ! Establish referential integrity using SQL

Chapter 13 : Informatics Practices. Class XI ( As per CBSE Board) SQL Commands. New Syllabus Visit : python.mykvs.in for regular updates

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

CGS 3066: Spring 2017 SQL Reference

Chapter 3: Introduction to SQL

(ADVANCED) DATABASE SYSTEMS (DATABASE MANAGEMENTS) PROF. DR. HASAN HÜSEYİN BALIK (6 TH WEEK)

T-SQL Training: T-SQL for SQL Server for Developers

1.8 Database and data Data Definition Language (DDL) and Data Manipulation Language (DML)

SQL. SQL Data Manipulation: Queries

MIS2502: Data Analytics SQL Getting Information Out of a Database. Jing Gong

Follow these steps to get started: o Launch MS Access from your start menu. The MS Access startup panel is displayed:

SQL Server Administration Class 4 of 4. Activant Prophet 21. Basic Data Manipulation

Lecture 2: Introduction to SQL

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

30. Structured Query Language (SQL)

What is SQL? Designed to retrieve data from relational databases, as well as to build and administer those databases.

Transcription:

Introduction to SQL IT 5101 Introduction to Database Systems J.G. Zheng Fall 2011

Overview Using Structured Query Language (SQL) to get the data you want from relational databases Learning basic syntax and simple queries All examples can be used with the Northwind database 2

SQL: Introduction SQL is a standard command language use by relational DBMS to perform database operations Some facts about SQL English-like Case insensitive An ANSI standard Not considered to be a programming language Most venders have their own proprietary extensions, making SQL slightly different in different DBMS products 3

What Does SQL Do? DML - data manipulation language Retrieving data: database queries Manipulating data: insert, update, delete DDL - data definition language Defining and modifying data structures (schema): databases, tables, views, etc. DCL - data control language Control data access: permissions, etc. 4

Database Queries A query is a request for information from a database. SELECT statements are used to retrieve data The result of a query is generally a table (but not necessarily a relation) SQL SELECT basics Defining selection criteria Sorting Table join 5

SELECT: General Syntax SELECT [Column(s), or other expressions] FROM [Table(s)] *WHERE + *ORDER BY + 6

1.1 Select Columns Syntax SELECT * (or a list of columns) FROM TableName Example SELECT * FROM Products; * represents all columns in the table SELECT ProductName, UnitPrice FROM Products; Provide a list of columns separated by comma 7

Column Qualifier and Alias Qualifiers are used to distinguish column names when there is ambiguity SELECT Products.ProductName, Products.UnitPrice FROM Products A qualifier of table name Alias is used to give column another name SELECT UnitPrice*Quantity AS Total FROM [Order Details]; Use AS operator to Use square brackets if there is blank spaces in table or column names. give an alias. 8

2. Select Rows (Selection Criteria) Use WHERE clause to specify selection criteria Common comparison operators =, >, <, >=, <=, <>,!= Examples SELECT * FROM Products WHERE UnitPrice = 18; SELECT BookTitle, ListPrice FROM Products WHERE UnitPrice < 20; 9

2.1 Comparing with Constants Numbers (int, float, decimal, etc.) WHERE UnitPrice > 20.99 Text (char, varchar, nvarchar, etc.): use ' ' (single quote) for values WHERE ProductName = 'Tofu' // exact match WHERE ProductName > 'Chang' Data/Time: SQL Server uses '...' WHERE OrderDate < '08/30/2008' compared by alphabetical order, > means after Before the date, 12:00AM Boolean (bit, yes/no, etc.): 'TURE' or 1, 'FALSE' or 0 WHERE Discontinued = 1 WHERE Discontinued = 'true' 10

2.2 IN and BETWEEN IN (a value list): equals to any value in the list SELECT * FROM Products WHERE UnitPrice IN (18, 19, 21) BETWEEN min AND max: any value falls in the range, inclusive SELECT * FROM Products WHERE UnitPrice BETWEEN 10 AND 20 11

2.3 String Pattern Match: LIKE LIKE: keyword match, or fuzzy query _ (underscore): matching one single character % (percentage) wildcard: matching any multiple characters Comparison pattern is case insensitive Example Can I get all tofu related products? SELECT * FROM Products WHERE ProductName LIKE '%tofu%' Matching any characters before tofu Matching any characters after tofu 12

2.4 IS NULL NULL means missing value IS NULL, IS NOT NULL SELECT * FROM Orders WHERE ShipPostalCode IS NULL SELECT * FROM Orders WHERE ShipPostalCode IS NOT NULL 13

2.5 NOT Reversal criteria NOT (expression) Examples WHERE NOT UnitPrice > 20; WHERE NOT ProductName LIKE '%tofu%'; WHERE ProductName NOT LIKE '%tofu%'; WHERE NOT ShipPostalCode IS NULL; 14

2.6 Compound Conditions Use logical operators to connect multiple criteria AND: satisfy both conditions OR: satisfy either criterion Comparison precedence: AND has a higher precedence over OR Best practice: use parentheses () to explicitly define comparison order Examples WHERE UnitPrice <= 20 AND UnitPrice >= 10; WHERE ShippedDate ='8/2/1996' OR ShippedDate = '8/9/1996'; WHERE (City = 'London' OR City = 'Seattle') AND HireDate > '1/1/1993'; If () are not used, City = 'Seattle' AND HireDate > '1/1/1993' will be evaluated first, which is wrong. 15

3. Sorting Sorting query results ORDER BY Column(s) [ASC/DESC] ASC (ascending) is the default order Examples SELECT * FROM Products ORDER BY UnitPrice; SELECT * FROM Products ORDER BY 6; Multiple sorting SELECT * FROM Products ORDER BY CategoryID, UnitPrice DESC Column position can also be used (starting from 1) Sort by category first, then UnitPrice 16

4. Table Join Retrieving data from multiple tables The query result consists of columns from more than one table How do rows match from different tables? Cross Join: no need to match. Inner Join: use the foreign key constraint as the matching criteria 17

Table Join Effect Product Category A 1 B 2 FK Join Category Name 1 Canned 2 Drink 3 Fresh Product Category A Canned A Drink A Fresh B Canned B Drink B Fresh Product A B Cross Join: no row matching Category Canned Drink Inner Join: row matching based on foreign key 18

Table Join SQL Example What is the category name for each product? SELECT ProductName, CategoryName FROM Products, Categories Where Foreign key Products.CategoryID = Categories.CategoryID Primary key Use table.column format to avoid ambiguity CategoryID in which table? Joining/matching criteria: very important, don t forget! 19

3. Table Join Syntax Alternative SELECT ProductName, CategoryName FROM Products, Categories Where Products.CategoryID = Categories.CategoryID AND Discontinued = 0; VS. 1. Joining/matching criteria: very important, don t forget! 2. Table.Cloumn format is used to avoid ambiguity. SELECT ProductName, CategoryName FROM Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID WHERE Discontinued = 0; 20

Joining More Tables (1) For each order after April 1, 1998, I want to see what products (product name) were ordered, and how many. SELECT Orders.OrderID, OrderDate, ProductName, Quantity FROM Orders, [Order Details], Products WHERE Orders.OrderID = [Order Details].OrderID AND [Order Details].ProductID = Products.ProductID AND OrderDate > '1998-04-01' 21

Joining More Tables (2) For each order after April 1, 1998, I want to see what products (product name) were ordered, and how many. SELECT Orders.OrderID, OrderDate, ProductName, Quantity FROM Orders INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID INNER JOIN Products ON [Order Details].ProductID = Products.ProductID WHERE OrderDate > '1998-04-01' 22

Summary Key concepts Database query SQL: DML, DDL, DCL Join, cross join, inner join Key skills: write SQL SELECT statement to retrieve data; and know the result of a given SQL SELECT statement. Select columns, * Selection criteria in WHERE: comparison operators =, <, >, >=, <=, <>,!=, IN, BETWEEN, LIKE, IS NULL, NOT %, _ AND, OR ORDER BY Table join 23

More SQL Resources W3Schools SQL Tutorial http://www.w3schools.com/sql/ SQL Course http://sqlcourse.com/ http://sqlcourse2.com/ A gentle introduction to SQL http://sqlzoo.net/ Other http://www.youtube.com/watch?v=rpp28u_k9lk http://www.1keydata.com/sql 24