database systems cse 303 lecture 03:...

40
Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query CSE 303: Ashikur Rahman

Upload: vuongcong

Post on 15-Jul-2018

221 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Database SystemsCSE 303

Lecture 03: SQL

1

2016

Single Table Query

CSE 303: Ashikur Rahman

Page 2: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

SQL

• Data Definition Language (DDL)

– Create/alter/delete tables and their attributes

– Following lectures...

2

– Following lectures...

• Data Manipulation Language (DML)

– Query one or more tables – discussed next !

– Insert/delete/modify tuples in tables

CSE 303: Ashikur Rahman

Page 3: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Tables in SQL

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Product

Attribute namesTable name

3

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Tuples or rows CSE 303: Ashikur Rahman

Page 4: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

SQL Query

Basic form: (plus many many more bells and whistles)

4

Basic form: (plus many many more bells and whistles)

SELECT <attributes>FROM <one or more relations>

CSE 303: Ashikur Rahman

Page 5: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Simplest SQL Query

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

Product

5

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Show all entries from product table

CSE 303: Ashikur Rahman

Page 6: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Simplest SQL Query

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Product

6

MultiTouch $203.99 Household Hitachi

SELECT *FROM Product

“selection”

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household HitachiCSE 303: Ashikur Rahman

Page 7: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Single Column Selection

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Product

Show all product categories

7

MultiTouch $203.99 Household Hitachi

SELECT categoryFROM Product

Category

Gadgets

Gadgets

Photography

Household

“selection” and“projection”

CSE 303: Ashikur Rahman

Page 8: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Single Column Selection (Eliminating Duplicates)

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Product

8

MultiTouch $203.99 Household Hitachi

SELECT DISTINCT categoryFROM Product

Category

Gadgets

Photography

Household

CSE 303: Ashikur Rahman

Show all product categories

Page 9: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

In summary..

SELECT DISTINCT categoryFROM Product

Category

Gadgets

Photography

Household

99

Compare to:

SELECT categoryFROM Product

Category

Gadgets

Gadgets

Photography

Household

CSE 303: Ashikur Rahman

Page 10: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Multi-column Selection

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Product

List all products along with their price and manufacturers

10

MultiTouch $203.99 Household Hitachi

SELECT pname, price, manufacturerFROM Product

PName Price Manufacturer

Gizmo $19.99 GizmoWorks

Powergizmo $29.99 GizmoWorks

SingleTouch $149.99 Canon

MultiTouch $203.99 HitachiCSE 303: Ashikur Rahman

Page 11: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Notation

Product(PName, Price, Category, Manfacturer)

Input Schema

11

Product(PName, Price, Category, Manfacturer)

Answer(PName, Price, Manfacturer)

Output Schema

SELECT pname, price, manufacturerFROM Product

CSE 303: Ashikur Rahman

Page 12: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Duplicates from multiple columnMaker Model Type

A 1001 pc

A 1002 pc

A 1003 pc

A 2004 laptop

A 2005 laptop

SELECT maker, typeFROM Product

1212

A 2005 laptop

A 2006 laptop

B 1004 pc

B 1005 pc

B 1006 pc

B 2007 laptop

C 1007 pc

… … …

…. … …

?CSE 303: Ashikur Rahman

Page 13: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Duplicates from multiple column

SELECT maker, typeFROM Product

Maker Type

A pc

A pc

A pc

A laptop

A laptop

1313CSE 303: Ashikur Rahman

A laptop

A laptop

B pc

B pc

B pc

B laptop

C pc

… …

…. …

Page 14: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Duplicates from multiple columnMaker Type

A pc

A pc

A pc

A laptop

A laptop

SELECT DISTINCT maker, typeFROM Product

1414

A laptop

A laptop

B pc

B pc

B pc

B laptop

C pc

… …

…. …

CSE 303: Ashikur Rahman

Page 15: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Duplicates from multiple columnMaker Type

A pc

A laptop

B pc

B laptop

C pc

1515

C pc

… …

…. …

… …

…. …

… …

…. …

… …

…. …

CSE 303: Ashikur Rahman

Page 16: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Simple Predicates

1616

SELECT <attributes>FROM <one or more relations>WHERE <conditions>

1

2

3

CSE 303: Ashikur Rahman

Page 17: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Semantics

SELECT a1,…,ak

FROM RWHERE conditions

1

2

3

1717CSE 303: Ashikur Rahman

Answer = {}for each x in R do

if conditions satisfied thenAnswer = Answer {(a1,…,ak)}

return Answer

Page 18: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Number Range Selection

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Product

Show all product’s info that will cost more than 100 dollars

1818

MultiTouch $203.99 Household Hitachi

SELECT *FROM ProductWHERE price > 100

PName Price Category Manufacturer

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

CSE 303: Ashikur Rahman

Answer = {}for each x in R do

if conditions satisfied thenAnswer = Answer {(a1,…,ak)}

return Answer

Page 19: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Number Range Operators

• Greater than “>”

• Less than “<”

19

• Less than “<”

• Greater than or equal to “>=”

• Less than or equal to “<=”

• Two forms of not-equal-to

“!=” and “< >”

CSE 303: Ashikur Rahman

Page 20: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

BETWEEN-AND operator

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Product

Show all product categoriesShow all product’s info in the price range $100 to $200 (inclusive)

2020

MultiTouch $203.99 Household Hitachi

SELECT *FROM ProductWHERE price BETWEEN 100 AND 200

PName Price Category Manufacturer

SingleTouch $149.99 Photography CanonBETWEEN-ANDis inclusive

CSE 303: Ashikur Rahman

Page 21: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

String Matching

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Product

Show all household product’s info

2121

MultiTouch $203.99 Household Hitachi

SELECT pname, price, manufacturerFROM ProductWHERE category =‘Household’

PName Price Manufacturer

MultiTouch $203.99 Hitachi

CSE 303: Ashikur Rahman

Matches even category is fixed length CHAR data type right padded with blank

Page 22: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

The LIKE operator

• s LIKE p: pattern matching on strings

• p may contain two special symbols:

(1) % any sequence of characters (zero or more)

22

(1) % any sequence of characters (zero or more)

(2) _ any single character (exactly one)

CSE 303: Ashikur Rahman

Page 23: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

The LIKE operator

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Show all product’s info that has ‘mo’ as a substring of the product name

23

SELECT *FROM ProductsWHERE pname LIKE ‘%mo%’

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

CSE 303: Ashikur Rahman

Page 24: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

The LIKE operator

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Show all product’s info where the product name ends with ‘Touch’

24

SELECT *FROM ProductsWHERE pname LIKE ‘%Touch’

PName Price Category Manufacturer

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

CSE 303: Ashikur Rahman

Page 25: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

The LIKE operatorPName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

25

SELECT *FROM ProductsWHERE manufacturer LIKE ‘G_z_oWorks’

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

CSE 303: Ashikur Rahman

Page 26: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Searching for three special symbols

• ’ ‘Baby’s World’

• % ‘10%’

26

• % ‘10%’

• _ ‘USA_Canada’

CSE 303: Ashikur Rahman

Page 27: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Searching for three special symbols

• ’ ‘Baby’s World’

Search for all company names having apostrophe s in the

27

Search for all company names having apostrophe s in the name?

Use double apostrophe

SELECT *

FROM Company

WHERE cname LIKE ‘%’’s%’

CSE 303: Ashikur Rahman

Page 28: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Searching for three special symbols

• % ‘10%’

Search for all interest rates having percent at the end?

28

Search for all interest rates having percent at the end?

Use ESCAPE characters

SELECT *

FROM Bank

WHERE interestRates LIKE ‘%\%’ ESCAPE ‘\’

CSE 303: Ashikur Rahman

Page 29: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Searching for three special symbols

• ‘_’ ‘USA_Canada’

Search for all country names having underscore inside?

29

Search for all country names having underscore inside?

Use ESCAPE characters

SELECT *

FROM Company

WHERE country LIKE ‘%\_%’ ESCAPE ‘\’

CSE 303: Ashikur Rahman

Page 30: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

LIKE does not pad with blank before matching

SELECT pname, price, manufacturerFROM ProductWHERE category =‘Household’

CSE 303: Ashikur Rahman 30

WHERE category =‘Household’

SELECT pname, price, manufacturerFROM ProductWHERE category LIKE ‘Household’

Not equivalent

Page 31: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

The NOT LIKE operatorPName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

3131

SELECT *FROM ProductsWHERE manufacturer NOT LIKE ‘%mo%’

PName Price Category Manufacturer

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

CSE 303: Ashikur Rahman

Page 32: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

Product

Combining Predicates

3232

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Show all household products having prices more than $100

CSE 303: Ashikur Rahman

Page 33: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Combining Predicates

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Product

Show all manufacturer that make photography or household items

333333

MultiTouch $203.99 Household Hitachi

SELECT DISTINCT makerFROM ProductWHERE category =‘Photography’OR category =‘Household’

Manufacturer

Canon

HitachiCSE 303: Ashikur Rahman

Page 34: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Combining Predicates

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Product

Show all manufacturer that make both photography and household items

WRONG!

343434

MultiTouch $203.99 Household Hitachi

SELECT DISTINCT makerFROM ProductWHERE category =‘Photography’AND category =‘Household’

PName Price Category Manufacturer

CSE 303: Ashikur Rahman

Page 35: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Combining PredicatesPName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

SELECT *

Product

353535

SELECT *FROM ProductWHERE pname LIKE ‘%Giz%’OR category LIKE ‘%hold’AND price > 100

Pname Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

MultiTouch $203.99 Household Hitachi

AND has higher precedence over OR

CSE 303: Ashikur Rahman0

Page 36: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Combining PredicatesPName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

SELECT *

Product

363636

SELECT *FROM ProductWHERE (pname LIKE ‘%Giz%’OR category LIKE ‘%hold’)AND price > 100

Pname Price Category Manufacturer

MultiTouch $203.99 Household Hitachi

CSE 303: Ashikur Rahman

Page 37: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

Ordering the Results

SELECT pname, price, manufacturer

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Default order is AscendingUse DESC for descending

37

SELECT pname, price, manufacturerFROM ProductWHERE price > 50ORDER BY pname

CSE 303: Ashikur Rahman

Pname Price Category Manufacturer

MultiTouch $203.99 Household Hitachi

SingleTouch $149.99 Photography Canon

Page 38: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

ORDER BY: Sorting the Results

SELECT PName, Price, ManufacturerFROM ProductWHERE Category=‘gizmo’ AND Price > 50ORDER BY Price, PName

CSE 303: Ashikur Rahman 38

ORDER BY Price, PName

Ties are broken by the second attribute on the ORDER BY list, etc.

Ties are broken by the second attribute on the ORDER BY list, etc.

Ordering is ascending, unless you specify the DESC keyword.

Ordering is ascending, unless you specify the DESC keyword.

Page 39: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

IN operator

artist album tracks company year

U2 The Unforgettable 10 Island 1984

U2 Rattle and Hum 17 Island 1988

U2 Achtung Boys 12 Island 1991

Underworld Second Toughest 8 Junior 1996

CDCollection

Find all albums launched in 1984 or 1996.

3939

SELECT albumFROM CDCollectionWHERE year IN (1984, 1996)

artist album tracks company year

U2 The Unforgettable 10 Island 1984

Underworld Second Toughest 8 Junior 1996

SELECT albumFROM CDCollectionWHERE year =1984OR year =1996 CSE 303: Ashikur Rahman

Page 40: Database Systems CSE 303 Lecture 03: SQLteacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_3_SingleTableQuery.… · Database Systems CSE 303 Lecture 03: SQL 1 2016 Single Table Query

NOT IN operatorartist album tracks company year

U2 The Unforgettable 10 Island 1984

U2 Rattle and Hum 17 Island 1988

U2 Achtung Boys 12 Island 1991

Underworld Second Toughest 8 Junior 1996

CDCollection

4040

SELECT *FROM CDCollectionWHERE year NOT IN (1984, 1996)

artist album tracks company year

U2 Rattle and Hum 17 Island 1988

U2 Achtung Boys 12 Island 1991

SELECT *FROM CDCollectionWHERE year < > 1984AND year < > 1996

CSE 303: Ashikur Rahman