sql : query language

of 24/24
CS3431 SQL : Query Language Part II

Post on 23-Jan-2016

37 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

SQL : Query Language. Part II. Sorting: ORDER BY clause. SELECT * FROM Student WHERE sNumber >= 1 ORDER BY sNumber, sName.  (sNumber, sName) ( (sNumber >= 1) (Student)). Subqueries. Subquery returns a relation Nest subqueries into WHERE clause Nest subqueries into FROM clause. - PowerPoint PPT Presentation

TRANSCRIPT

  • CS3431SQL : Query LanguagePart II

    CS3431

  • CS3431Sorting: ORDER BY clauseSELECT * FROM StudentWHERE sNumber >= 1ORDER BY sNumber, sName

    (sNumber, sName) ( (sNumber >= 1) (Student))

    CS3431

  • CS3431SubqueriesSubquery returns a relation

    Nest subqueries into WHERE clause

    Nest subqueries into FROM clause

    CS3431

  • CS3431SubqueriesSELECT * FROM StudentWHERE professor = (SELECT pNameFROM ProfessorWHERE pNumber=1)Note: The inner subquery returns a relation, but SQL runtime ensures that subquery returns a relation with one column and with one row, otherwise it is a run-time error.

    CS3431

  • CS3431Subqueries - ExampleStudentProfessorSELECT * FROM StudentWHERE professor = (SELECT pNameFROM ProfessorWHERE pNumber=1)

    sNumbersNameaddressprofessor1Dave320FLMM2Greg320FLMM3Matt320FLER

    pNumberpNameaddress1MM141FL2ER201FL

    sNumbersNameaddressprofessor1Dave320FLMM2Greg320FLMM

    CS3431

  • CS3431SubqueriesWe can use IN, EXISTS, NOT IN, and NOT EXISTSALL, ANY can be used with comparisonsSELECT * FROM StudentWHERE (sNumber, professor) IN(SELECT pNumber, pName FROM Professor)

    CS3431

  • CS3431Subqueries - ExampleStudentProfessorSELECT * FROM StudentWHERE (sNumber, professor) IN(SELECT pNumber, pName FROM Professor)

    sNumbersNameaddressprofessor1Dave320FLMM2Greg320FLMM3Matt320FLER

    pNumberpNameaddress1MM141FL2ER201FL

    sNumbersNameaddressprofessor1Dave320FLMM

    CS3431

  • CS3431Subqueries: EXISTSSELECT * FROM StudentWHERE EXISTS(SELECT pName FROM Professor WHERE Student.professor=pName)StudentProfessor

    sNumbersNameaddressprofessor1Dave320FLMM2Greg320FLMM3Matt320FLER

    pNumberpNameaddress1MM141FL2ER201FL

    sNumbersNameaddressprofessor1Dave320FLMM2Greg320FLMM3Matt320FLER

    CS3431

  • CS3431Subqueries with negationSELECT * FROM StudentWHERE (sNumber, professor) NOT IN(SELECT pNumber, pName FROM Professor)ProfessorStudent

    pNumberpNameaddress1MM141FL2ER201FL

    sNumbersNameaddressprofessor1Dave320FLMM2Greg320FLMM3Matt320FLER

    sNumbersNameaddressprofessor2Greg320FLMM3Matt320FLER

    CS3431

  • CS3431Subqueries with negationSELECT * FROM StudentWHERE NOT EXISTS(SELECT pName FROM Professor WHERE Student.professor=pName)StudentProfessor

    sNumbersNameaddressprofessor1Dave320FLMM2Greg320FLMM3Matt320FLER

    pNumberpNameaddress1MM141FL2ER201FL

    sNumbersNameaddressprofessor

    CS3431

  • CS3431Subqueries: ALL, ANY

    CS3431

  • CS3431Subqueries: ALL - ExampleSELECT * FROM StudentWHERE sNumber > ALL(SELECT pNumber FROM Professor)StudentProfessor

    sNumbersNameaddressprofessor1Dave320FLMM2Greg320FLMM3Matt320FLER

    pNumberpNameaddress1MM141FL2ER201FL

    sNumbersNameaddressprofessor3Matt320FLER

    CS3431

  • CS3431Subqueries: ANY - ExampleSELECT * FROM StudentWHERE sNumber = ANY(SELECT pNumber FROM Professor)StudentProfessor

    sNumbersNameaddressprofessor1Dave320FLMM2Greg320FLMM3Matt320FLER

    pNumberpNameaddress1MM141FL2ER201FL

    sNumbersNameaddressprofessor1Dave320FLMM2Greg320FLMM

    CS3431

  • CS3431Subqueries: NOT ALL - ExampleSELECT * FROM StudentWHERE NOT sNumber > ALL(SELECT pNumber FROM Professor)StudentProfessor

    sNumbersNameaddressprofessor1Dave320FLMM2Greg320FLMM3Matt320FLER

    pNumberpNameaddress1MM141FL2ER201FL

    sNumbersNameaddressprofessor1Dave320FLMM2Greg320FLMM

    CS3431

  • CS3431Subqueries: NOT ANY - ExampleSELECT * FROM StudentWHERE NOT sNumber = ANY(SELECT pNumber FROM Professor)StudentProfessor

    sNumbersNameaddressprofessor1Dave320FLMM2Greg320FLMM3Matt320FLER

    pNumberpNameaddress1MM141FL2ER201FL

    sNumbersNameaddressprofessor3Matt320FLER

    CS3431

  • CS3431Subqueries: Correlation

    CS3431

  • CS3431Subqueries in FROM clauseSELECT sName, pNameFROM Student, (SELECT * FROM Professor WHERE pNumber=1)WHERE professor=pName;ProfessorStudent

    pNumberpNameaddress1MM141FL2ER201FL

    sNumbersNameaddressprofessor1Dave320FLMM2Greg320FLMM3Matt320FLER

    sNamepNameDaveMMGregMM

    CS3431

  • CS3431Duplicate EliminationSELECT DISTINCT *FROM Student; (Student)SELECT DISTINCT addressFROM StudentWHERE sNumber >= 1; ( (address) ( (sNumber >= 1) (Student)))Student

    sNumbersNameaddressprofessor1Dave320FLMM2Greg320FLMM3Matt320FLER

    address320FL

    CS3431

  • CS3431Aggregation + GroupBy

    CS3431

  • CS3431Aggregation FunctionsSELECT COUNT (*) FROM Student;SELECT COUNT (sNumber) FROM Student;SELECT MIN (sNumber) FROM Student;SELECT MAX (sNumber) FROM Student;SELECT SUM (sNumber) FROM Student;SELECT AVG (sNumber) FROM Student;

    We can have distinct such as:SELECT COUNT (DISTINCT sNumber) FROM Student

    CS3431

  • CS3431GroupingSELECT COUNT (sName) as cnum FROM Student GROUP BY address;(COUNT (sName) as snum) ( (address, COUNT (sName)) (Student))Student

    sNumbersNameaddressprofessor1Dave320FLMM2Greg320FLMM3Matt320FLER

    COUNT (sName)3

    CS3431

  • CS3431GroupingSELECT address, COUNT (sNumber) FROM StudentWHERE sNumber > 1GROUP BY addressHAVING COUNT (sNumber) > 1;Student

    sNumbersNameaddressprofessor1Dave320FLMM2Greg320FLMM3Matt320FLER4Ben300FLER

    addressCOUNT (sNumber)320FL2

    CS3431

  • CS3431Aggregation and NULLsNULLs are ignored in any aggregation; except COUNT (*)However if the set of attributes to be grouped on has null values, then grouping is done on the null values as well.

    CS3431

  • CS3431SQL Queries - SummarySELECT [DISTINCT] a1, a2, , anFROM R1, R2, , Rm[WHERE C1][GROUP BY g1, g2, , gl [HAVING C2]][ORDER BY o1, o2, , oj]

    CS3431