Introductjon to SQL Part 1 – Single-Table Queries
By Michael Hahsler based on slides for CS145 Introductjon to Databases (Stanford)
Introductjon to SQL Part 1 Single-Table Queries By Michael Hahsler - - PowerPoint PPT Presentation
Introductjon to SQL Part 1 Single-Table Queries By Michael Hahsler based on slides for CS145 Introductjon to Databases (Stanford) Overview 1. SQL introductjon & schema defjnitjons 2. Basic single-table queries 2 1. SQL INTRODUCTION
By Michael Hahsler based on slides for CS145 Introductjon to Databases (Stanford)
2
3
4
5
– Standardized in 1986/87 – ANSI SQL/ SQL-86, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3), SQL:2011 – Vendors support various subsets (e.g., SQLite implements most of the SQL-92 standard)
SQL stands for Structured Query Language SQL stands for Structured Query Language
7
8
PName Price
Manufacturer
Gizmo $19.99
GizmoWorks
Powergizmo $29.99
GizmoWorks
SingleTouch $149.99
Canon
MultiTouch $203.99
Hitachi
10
PName Price
Manufacturer
Gizmo $19.99
GizmoWorks
Powergizmo $29.99
GizmoWorks
SingleTouch $149.99
Canon
MultiTouch $203.99
Hitachi
Atuributes must have an atomic type in standard SQL, i.e. not a list, set, etc. Atuributes must have an atomic type in standard SQL, i.e. not a list, set, etc.
11
PName Price
Manufacturer
Gizmo $19.99
GizmoWorks
Powergizmo $29.99
GizmoWorks
SingleTouch $149.99
Canon
MultiTouch $203.99
Hitachi
Sometjmes also referred to as a record Sometjmes also referred to as a record
13
14
(key candidates and primary key)
Students(sid: text, name: text, gpa: real) Students(sid: text, name: text, gpa: real)
sid name gpa 123 Bob 3.9 143 Jim
NULL
Students(sid:text, name:text, gpa: real) Students(sid:text, name:text, gpa: real)
19
PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi
20
21
22
23
PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SELECT * FROM Product WHERE Category = ‘Gadgets’ SELECT * FROM Product WHERE Category = ‘Gadgets’
24
PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi PName Price Manufacturer Gizmo $19.99 GizmoWorks Powergizmo $29.99 GizmoWorks
SELECT Pname, Price, Manufacturer FROM Product WHERE Category = ‘Gadgets’ SELECT Pname, Price, Manufacturer FROM Product WHERE Category = ‘Gadgets’
Projectjon is the
producing an output table with tuples that have a subset of their prior atuributes Projectjon is the
producing an output table with tuples that have a subset of their prior atuributes
25
SELECT Pname, Price, Manufacturer FROM Product WHERE Category = ‘Gadgets’ SELECT Pname, Price, Manufacturer FROM Product WHERE Category = ‘Gadgets’
Product(PName, Price, Category, Manfacturer) Product(PName, Price, Category, Manfacturer) Answer(PName, Price, Manfacturer) Answer(PName, Price, Manfacturer)
– Same: SELECT, Select, select – Same: Product, product
– Difgerent: ‘Seatule’, ‘seatule’
– ‘abc’ - yes – “abc” - no
26
27
Category Gadgets Gadgets Photography Household Category Gadgets Photography Household
28
29
Ties are broken by the second aturibute on the ORDER BY list, etc. Ties are broken by the second aturibute on the ORDER BY list, etc. Ordering is ascending, unless you specify the DESC keyword. Ordering is ascending, unless you specify the DESC keyword. Text is ordered alphabetjcally. Text is ordered alphabetjcally.
30
Some of the operators supported by SQL are: =, == equal !=, <> not equal <, <= less than (or equal) >, >= greater than (or equal) +, -, /, * arithmetjc operators AND, OR, NOT logic operators IS NULL, IS NOT NULL checks for NULL values Example: Find products and their price + 8% sales tax for gadgets that cost at least $100
31
The IN operator allows you to specify multjple values in a WHERE clause.
The BETWEEN operator selects values within a range. The values can be numbers, text,
32
– % = any sequence of characters – _ = any single character
33
34
SELECT name, CASE WHEN price > 200 THEN ‘Yes’ ELSE ‘No’ END AS expensive FROM Product SELECT name, CASE WHEN price > 200 THEN ‘Yes’ ELSE ‘No’ END AS expensive FROM Product
name category price Gizmo gadget 50 Camera Photo 299 OneClick Photo 89
(htup://www.tutorialspoint.com/sqlite/)
multjple tables!
35