SQL Structured Query Language 1 Best known and most commonly used - - PowerPoint PPT Presentation

sql structured query language
SMART_READER_LITE
LIVE PREVIEW

SQL Structured Query Language 1 Best known and most commonly used - - PowerPoint PPT Presentation

SQL Structured Query Language 1 Best known and most commonly used relational database query and manipulation language. Used in many commercial database systems. 2 SQL includes several components including: Data definition


slide-1
SLIDE 1

1

SQL Structured Query Language

slide-2
SLIDE 2

2

  • Best known and most commonly used

relational database query and manipulation language.

  • Used in many commercial database

systems.

slide-3
SLIDE 3

3

SQL includes several components including:

  • Data definition
  • Query (interactive) Language
  • View definition
  • Integrity
  • Authorisation
  • Transaction control
slide-4
SLIDE 4

4

SQL as a Query Language

Basic query structure: SELECT Attributes FROM Relations WHERE Conditions

slide-5
SLIDE 5

5

SELECT A1, …, An FROM R1, …, Rm WHERE C The WHERE part may be missing.

slide-6
SLIDE 6

6

Example: SELECT City, Status FROM S WHERE Sname = ‘Smith’

slide-7
SLIDE 7

7

Result

City Status London 20 _______________ (1 row)

slide-8
SLIDE 8

8

Querying One Relation

Examples: Get the names of all the suppliers in London. SELECT Sname FROM S WHERE City = ‘London’

slide-9
SLIDE 9

9

Get the names and cities of all the suppliers with status higher than 10. SELECT Sname, City FROM S WHERE Status>10

slide-10
SLIDE 10

10

Use of asterisk (*)

SELECT * FROM P WHERE City = ‘London’ The asterisk (*) means that all columns of the table P are to be presented.

slide-11
SLIDE 11

11

The Use of IN and NOT IN

The condition in WHERE clauses can refer to a set of values. To do this the keywords IN and NOT IN may be used. Examples: SELECT Sname FROM S WHERE City IN [‘London’, ‘Paris’]

slide-12
SLIDE 12

12

In does not add much to SQL expressive power. SELECT Sname FROM S WHERE City=‘London’ OR City= ‘Paris’; IN is more efficient when set contains many values.

slide-13
SLIDE 13

13

SELECT Sname FROM S WHERE City NOT IN [‘Athens’, ‘Rome’]

slide-14
SLIDE 14

14

IS NULL

SELECT EMP#, Name FROM Emp WHERE MgrEmp# IS NULL Negated version IS NOT NULL tests for non-NULL values.

slide-15
SLIDE 15

15

Removing duplicates in answers: DISTINCT

SELECT DISTINCT Status FROM S

slide-16
SLIDE 16

16

Result Status 10 20 30 _____ (3 rows)

slide-17
SLIDE 17

17

Sorting

The rows of the resulting relations can be sorted by the values in one or more columns. SELECT Sname, City, Status FROM S ORDER BY Status The above query will present the result in ascending order of “status”.

slide-18
SLIDE 18

18

Result Sname City Status Jones Paris 10 Smith London 20 Clark London 20 Blake Paris 30 Adams Athens 30 (5 rows)

slide-19
SLIDE 19

19

SELECT S#, P#, Qty From SP ORDER BY P# ASC, Qty DESC

slide-20
SLIDE 20

20

Result S# P# Qty S1 P1 300 S2 P1 300 S2 P2 400 S1 P2 200 S3 P2 200 S4 P2 200 S1 P3 400 S4 P4 300 S1 P4 200 S4 P5 400 S1 P5 100 S1 P6 100 (12 ROWS)

slide-21
SLIDE 21

21

SQL Built-in Aggregate Functions

SQL provides 5 standard built-in aggregate functions:

  • COUNT
  • SUM
  • AVG
  • MAX
  • MIN
slide-22
SLIDE 22

22

  • Each returns a single value.
  • COUNT, MIN, MAX apply to numeric and

non-numeric fields.

  • SUM, AVG apply to numeric fields only.
  • Apart from COUNT, each function

eliminates NULL values first and operates

  • nly on the remaining non-NULL values.
slide-23
SLIDE 23

23

SELECT COUNT(*) From S This counts the number of the rows in table S, and displays the number in a table which has a single row and a single column.

slide-24
SLIDE 24

24

The AS clause to name columns

SELECT COUNT(*) AS no_of_suppliers From S

slide-25
SLIDE 25

25

Result no_of_suppliers _____________ 5 _____________ (1 row)

slide-26
SLIDE 26

26

SELECT COUNT (DISTINCT S#) From SP This counts the number of distinct suppliers we have orders with according to table SP.

slide-27
SLIDE 27

27

Result Col1 4 ______ (1 row)

slide-28
SLIDE 28

28

Built-in Aggregate Functions and Grouping

To increase their utility, the built-in aggregate functions can be applied to groups of rows within a table. Such groups are formed by considering those rows that have the same value for a specified column.

slide-29
SLIDE 29

29

SELECT P#, SUM(Qty) AS Orders FROM SP GROUP BY P# This gives the sum of all quantities on

  • rder for each part number.
slide-30
SLIDE 30

30

Result P# Orders P1 600 P2 1000 P3 400 P4 500 P5 500 P6 100 (6 rows)

slide-31
SLIDE 31

31

SELECT P#, COUNT(*) FROM SP GROUP BY P# HAVING COUNT(*)>2 This gives the total number of orders for each part which has more than two

  • rders.
slide-32
SLIDE 32

32

Result P# col2 P2 4 (1 row)

slide-33
SLIDE 33

33

Querying Multiple Tables

Examples of using product, natural join, union: Get supplier names for suppliers who supply part P2. SELECT S.Sname FROM S, SP WHERE S.S#=SP.S# AND SP.P# ='P2'

slide-34
SLIDE 34

34

Cartesian Product of Relations

R1 R2 A B B C 1 b1 b1 c1 2 b2 b3 c3

slide-35
SLIDE 35

35

R1*R2

R1.A R1.B R2.B R2.C 1 b1 b1 c1 1 b1 b3 c3 2 b2 b1 c1 2 b2 b3 c3

slide-36
SLIDE 36

36

R1 NATURAL JOIN R2

R1.A R1.B R2.B R2.C 1 b1 b1 c1 1 b1 b3 c3 2 b2 b1 c1 2 b2 b3 c3 A B C 1 b1 c1

slide-37
SLIDE 37

37

SELECT S.Sname FROM S NATURAL JOIN SP WHERE SP.P# ='P2'

slide-38
SLIDE 38

38

Get the names and quantities of parts supplied by S1. SELECT P.Pname, SP.Qty FROM P, SP WHERE P.P# = SP.P# AND SP.S# = ‘S1’

slide-39
SLIDE 39

39

Get part numbers of parts that are either stored in London or are supplied by S1, or both. SELECT P.P# FROM P WHERE P.City='LONDON' UNION SELECT SP.P# FROM SP WHERE SP.S#='S1'

slide-40
SLIDE 40

40

The rename operation

SELECT SP1.S# FROM SP SP1, SP SP2 WHERE SP2.S# = S1 AND SP1.P# = P1 AND SP2.P# = P1 AND SP1.Qty>SP2.Qty

slide-41
SLIDE 41

41

The Use of Subqueries

SELECT S.Sname FROM S WHERE S.S# IN (SELECT SP.S# FROM SP WHERE SP.P# ='P2') Notice that we could have done the above query with a product or a join.

slide-42
SLIDE 42

42

SELECT S.Sname FROM S, SP WHERE S.S# = SP.S# AND SP.P# ='P2'

slide-43
SLIDE 43

43

Comparison of Sunqueries and Products/Joins

  • Joins or products can be used instead
  • f all subqueries, but in some cases

subqueries may be more “natural” to use (for example those involving EXISTS and NOT EXISTS - described below).

slide-44
SLIDE 44

44

  • Subqueries cannot be used to express

all joins/products. In particular in a join/product, the displayed columns may come from any of the joined

  • relations. But when using a subquery,

the displayed columns may come from

  • nly the relation named in the FROM

expression of the first SELECT.

slide-45
SLIDE 45

45

EXISTS and NOT EXISTS

EXISTS and NOT EXISTS are logical expressions which have truth values (false or true). Examples: Get supplier names of those suppliers which supply part P2.

slide-46
SLIDE 46

46

SELECT S.Sname FROM S WHERE EXISTS (SELECT * FROM SP WHERE S.S#=SP.S# AND SP.P# ='P2')

slide-47
SLIDE 47

47

Get supplier names for suppliers who do not supply part p2. SELECT S.Sname FROM S WHERE NOT EXISTS (SELECT * FROM SP WHERE S.S#=SP.S# AND SP.P# ='P2')

slide-48
SLIDE 48

48

Get the supplier numbers of those suppliers who supply at lease two different parts. SELECT S.S# FROM SP SP1 WHERE EXISTS (SELECT * FROM SP SP2 WHERE SP1.S#=SP2.S# AND SP1.P# NOT=SP2.P#)

slide-49
SLIDE 49

49

Some Additional Features

Consider relation FILM, as below: FILM(Name, Director, Producer) SELECT Producer, Director FROM FILM WHERE Name LIKE %mountain Also %third% sequence of characters of any length containing ‘third’

slide-50
SLIDE 50

50

SELECT Director FROM FILM WHERE Name LIKE _ _ _ _ _ _ : any single character

slide-51
SLIDE 51

51

Summary: SELECT

SELECT [DISTINCT | ALL] {* | [column_expression [AS new_name]] [,...] } FROM table_name [alias] [, ...] [WHERE condition] [GROUP BY column_list] [HAVING condition] [ORDER BY column_list]

slide-52
SLIDE 52

52

SELECT Specifies which columns are to appear in output. FROM Specifies table(s) to be used. WHERE Filters rows. GROUP BY Forms groups of rows with same column value. HAVING Filters groups subject to some condition. ORDER BY Specifies the order of the

  • utput.
slide-53
SLIDE 53

53

  • Order
  • f

the clauses cannot be changed.

  • Only

SELECT and FROM are mandatory.