1 / 27

lecture 3 : SQL - Queries

course: Database Systems ( A7B36DBS, Y36DBS, X36DBS ). lecture 3 : SQL - Queries. Lecturers: RNDr. Irena Mlynkova, Ph.D. & Mgr. Martin Necasky, Ph.D. Acknowledgement: The slides were kindly lent by Doc. RNDr. Tomas Skopal, Ph.D.,

ayala
Download Presentation

lecture 3 : SQL - Queries

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. course: Database Systems(A7B36DBS, Y36DBS, X36DBS) lecture3: SQL - Queries Lecturers: RNDr. Irena Mlynkova, Ph.D. & Mgr. Martin Necasky, Ph.D. Acknowledgement: The slides were kindly lent by Doc. RNDr. Tomas Skopal, Ph.D., Department of Software Engineering, Charles University in Prague

  2. Today’s lecture outline • introduction to SQL • queries • SELECT command • sorting • set operations SQL – queries (Lect. 3)

  3. SQL • structured query language • standard language for access to (relational) databases • originally ambitions to provide “natural language”(that’s why, e.g., SELECT is so complex – a single phrase) • also language for • data definition (DDL) • creation and altering of relational (table) schemas • data manipulation (DML) • querying • data insertion, deletion, updating • transaction management • modules(programming language) • definition of integrity constraints • administration SQL – queries (Lect. 3)

  4. SQL • standards ANSI/ISO SQL 86, 89, 92, 1999, 2003 (backwards compatible) • commercial systems implement SQL at different standard level (most often SQL 99, 2003) • unfortunately, not strict implementation – some extra nonstandard features supported while some standard ones not supported • specific extensionsprocedural, transactionaland other functionality,e.g., TRANSACT-SQL (Microsoft SQL Server), PL/SQL (Oracle) SQL – queries (Lect. 3)

  5. SQL evolution • SQL 86 – first „shot“, intersection of IBM SQL implementations • SQL 89 – smallrevisiontriggered byindustry, manydetailsleft for 3rd parties • SQL 92 – strongerlanguage, specification 6x longerthanfor SQL 86/89 • schema modification, tableswith metadata, inner joins, cascade deletes/updates based on foreign keys, setoperations, transactions, cursors, exceptions • foursubversions – Entry, Transitional, Intermediate, Full • SQL 1999 – manynewfeatures, e.g., • object-relationalextensions • types STRING, BOOLEAN, REF, ARRAY, typesfor full-text, images, spatial data • triggers, roles, programming language, regularexpressions, recursive queries, etc. • SQL 2003 – furtherextensions, e.g., XML management, autonumbers, std. sequences, but also type BIT removed SQL – queries (Lect. 3)

  6. Queries in SQL • for simplicity we consider SQL 86 syntax, and use syntax diagrams source: prof. H.J. Schek (ETH Zurich) • oriented graph (DFA automatonaccepting SQL) • termsin diagrams • smallletters, underlined – subexpression • capital letters –SQL keyword • smallletters, italic – name (table/column/...) • we use usual terminology table/column instead of relation/attribute • diagrams do not include ANSI SQL 92 syntax for joins(clauses CROSS JOIN, NATURAL JOIN, INNER JOIN, LEFT | RIGHT | FULL OUTER JOIN, UNION JOIN) – shown later SQL – queries (Lect. 3)

  7. Basic query construction • unorderedqueryconsists of • mandatory commandSELECT (main query logic) • commandsUNION, INTERSECTION, EXCEPTwhich specifyunion/intersection/subtraction of two or more results obtained by query defined by SELECT command • elements in resultshave no defined order (theirorderis determined by the implementation of query evaluation, resp.) SQL – queries (Lect. 3)

  8. Orderedquery • the result of unorderd query can be ordered/sorted • clauseORDER BY, orderingaccording to column(s) • ascending (ASC) or descending (DESC) order • multiple secondary columns can be specified that apply in case of duplicate values in the primary (higher secondary) columns SQL – queries (Lect. 3)

  9. SELECT • SELECTcommand consists of 2-5 clauses (+ optionallyORDER BY) • clauseSELECT– projectioninto output schema, or definition of new, derived, aggregated columns • clauseFROM– which tables (in case of SQL  99 also nested queries, views) we query • clauseWHERE – condition that must a row (record) satisfy in order to get into the result • clauseGROUP BY – which attributes should serve for aggregation • clauseHAVING – condition that must an aggregated row (record) satisfy in order to get into the result SQL – queries (Lect. 3)

  10. SELECT – diagram Logicalorderof evaluation (associativity of SELECT clauses, resp.): FROM  WHEREGROUP BY HAVING  projection SELECT ( ORDER BY) SQL – queries (Lect. 3)

  11. SELECT – expression Context: SELECT ALL | DISTINCT expression FROM ... multiple times insidesearch_condition SQL – queries (Lect. 3)

  12. SELECT – search condition Context: SELECT ... FROM ... WHERE search_condition SELECT ... FROM ... WHERE ... GROUP BY ... HAVING search_condition SQL – queries (Lect. 3)

  13. Tables used in the examples tabuleFlights tablePlanes SQL – queries (Lect. 3)

  14. SELECT ... FROM ... • simplestquery:SELECT [ALL] | DISTINCT expression FROMtable1, table2, ... • the expressionmightcontain • columns (* is surrogate forallcolumns) • constants • aggregation functions on expressions • if there is at least one aggregation, in the expression we can use only the aggregated columns (thosespecified inclauseGROUP BY), while other columns must be „wrapped“ intoaggregation functions • if the clauseGROUP BY is not defined, the aggregation results in a single group(all the sources defined in the clause FROM is aggregated into single row) • DISTINCTeliminates suplicaterows in the output, ALL (default) allows duplicate rows in the output (note that this affects aggregations – withDISTINCTthere is less values entering the aggregation) • FROMcontains one or more tables that serve as the source for the query • if there is multiple tables specified, the cartesian product is created SQL – queries (Lect. 3)

  15. Examples – SELECT ... FROM ... Which companies are in service? SELECT DISTINCT‘Comp.:’, CompanyFROMFlights What plane pairs can be created (regardless the owner) and what is their capacity: SELECTL1.Plane, L2.Plane, L1.Capacity+ L2.CapacityFROMPlanesASL1, Planes ASL2 SQL – queries (Lect. 3)

  16. Examples – SELECT ... FROM ... Howmany companies are in service? SELECT COUNT(DISTINCT Company)FROMFlights How many flights were accomplished? SELECT COUNT(Company)FROMFlights SELECT COUNT(*)FROMFlights How many planes is in the fleet,what is their max, min, average andtotalcapacity? SELECT COUNT(*), MAX(Capacity), MIN(Capacity), AVG(Capacity), SUM(Capacity)FROMPlanes SQL – queries (Lect. 3)

  17. SELECT ... FROM ... WHERE ... • selection condition, i.e.,table row (or row of a join) for which the condition is truegets into the result • simple conditions can be combined by AND, OR, NOT • predicates • (in)equation predicate (=, <>, <, >, <=, >=) on values of two columns • interval predicate – expr1 [NOT]BETWEEN (expr2 AND expr3) • string matchingpredicate[NOT]LIKE “mask“, where% in the mask represents an arbitrary substring,and_representsan arbitrary character • NULL value predicate – (expr1)IS [NOT] NULL • set membership predicate – expr1[NOT] IN (unordered_query) • empty set predicate – EXISTS(unordered_query) • extended quantifiers • existence quantifierexpr1= |<> | < | > | <= | >=ANY (unordered_query) • i.e., at leastone row in unordered_queryresults in true when compared with expr1 • universal quantifierexpr1 = |<> | < | > | <= | >=ALL (unordered_query) • i.e., all rows in unordered_queryresult in true when compared with expr1 SQL – queries (Lect. 3)

  18. Examples – SELECT ... FROM ... WHERE... What flights are occupied by more than 100 passengers? SELECT Flight, PassengersFROMFlightsWHEREPassengers> 100 Which planesare ready to fly if we require utilization of a plane at least 30%? SELECT Flight, Plane, (100 * Flights.Passengers/ Planes.Capacity) ASUtilizationFROMFlights, PlanesWHERE Flights.Company = Planes.CompanyAND Flights.Passengers<= Planes.Capacity AND Utilization >= 30 SQL – queries (Lect. 3)

  19. Examples – SELECT ... FROM ... WHERE ... Which destinations can fly Airbus of any company (regardless utilization)? SELECT DISTINCT DestinationsFROMFlightsWHEREFlights.CompanyIN(SELECT CompanyFROMPlanesWHEREPlaneLIKE“Airbus%”) or SELECT DISTINCT DestinationFROMFlights, PlanesWHEREFlights.Company= Planes.CompanyAND PlaneLIKE“Airbus%” Passengers of which flights fit any plane (regardless plane owner)? SELECT *FROMFlightsWHEREPassengers<= ALL (SELECT Capacity FROM Planes) SQL – queries (Lect. 3)

  20. Joins How to join without special language constructions (SQL 86): • (inner) joinon condition andnaturaljoincan be realized as restricted cartesian product, i.e.,SELECT ... FROM table1, table2 WHERE table1.A = table2.B • left/rightsemi-joinis specified in clause SELECT, i.e., byprojection New SQL 92 constructions: • cartesianproduct – SELECT ... FROM table1 CROSS JOIN table2 ... • natural join – SELECT ... FROM table1 NATURAL JOIN table2 WHERE ... • innerjoin – SELECT ... FROM table1 INNER JOIN table2 ONsearch_conditionWHERE ... • unionjoin – returnsrows of the first tablehaving NULLs in attributes of the second one+ the same with second tableSELECT ... FROM table1 UNION JOIN table2 WHERE ... • left, right, fullouterjoin – SELECT ... FROM table1 LEFT | RIGHT | FULL OUTER JOIN table2ONsearch_condition ...WHERE ... SQL – queries (Lect. 3)

  21. Examples – joins, ORDER BY Get pairs flight-planesorted in ascendant order w.r.t.unoccupiedspacein plane when the passengers board (consider just the planes the passengers can fit in)? SELECTFlight, Plane, (Capacity – Passengers) ASUnoccupiedFROMFlightsINNER JOINPlanesON(Flights.Company = Planes.CompanyANDPassengers<=Capacity) ORDER BY Unoccupied Which flights cannot be operated (because the companydoes not ownany/suitableplane)? SELECT Flight, DestinationFROMFlightsLEFT OUTER JOINPlanesON(Flights.Company = Planes.CompanyAND Passengers<=Capacity) WHEREPlanes.CompanyIS NULL SQL – queries (Lect. 3)

  22. SELECT /.../ GROUP BY ... HAVING ... • aggregationclausesthat group the rows of the result table after FROM andWHEREbased on identical values in defined columns into columnsgroups • the result is table of „superrows“, where the aggregatingcolumnshave defined values (as in the original rows), while the other columns must be created by aggregation (otherwise the values would be ambiguous) • generalization of aggregatingfunctionshown earlier(COUNT, MAX, MIN, AVG, SUM), where the result is not single-row table (as in case of not using GROUP BY), but a table of as many rows as the number of superrows is (after GROUP BY) • from this „superrow“ table we can filter out row based on HAVING clause, similarly as using WHEREafter FROM • note that HAVINGexpressioncan only use aggregated columns as in SELECT SQL – queries (Lect. 3)

  23. Examples – SELECT /.../ GROUP BY ... HAVING ... What (positive) transportcapacityhave the companies? SELECTCompany, SUM(Capacity) FROM Planes GROUP BY Company What companies can fit all their passengers into their planes (regardless of their destinations)? SELECTCompany, SUM(Passengers) FROMFlightsGROUP BYCompanyHAVING SUM(Passengers)<= (SELECTSUM(Capacity) FROM PlanesWHERE Flights.Company = Planes.Company) SQL – queries (Lect. 3)

  24. Nested queries • standard SQL92 (full) allows to use nestedqueriesalso in the FROMclause • while in SQL 86 the nested queries were allowedonly inANY, ALL, IN, EXISTS • two ways of application • SELECT ... FROM (unordered_query) ASq1WHERE ... • allows selection right from the subquery result (instead of table/join) • subquery result is calledq1whilethisidentifieris used in other clauses as it would be a table • SELECT ... FROM ((unordered_query)AS q1CROSS| NATURAL |INNER | OUTER | LEFT | RIGHT JOIN (unordered_query)AS q2ON (expression) • usage in joins of all kinds SQL – queries (Lect. 3)

  25. Examples – nested queries Get sums of passengers and plane capacities for all companies that own a plane. SELECT Flights.Company, SUM(Flights.Passengers), MIN(Q1.TotalCapacity) FROM Flights, (SELECTSUM(Capacity) AS TotalCapacity, Company FROM Planes GROUP BY Company)AS Q1 WHERE Q1.Company=Flights.Company GROUP BY Flights.Company; Get tripletsof different flights for which the numbers of passengers differ by max 50. SELECT Flights1.Flight, Flights1.Passengers, Flights2.Flight, Flights2.Passengers, Flights3.Flight, Flights3.PassengersFROMFlights ASFlights1INNER JOIN(Flights ASFlights2 INNER JOIN Flights ASFlights3 ON Flights2.Flight<Flights3.Flight) ON Flights1.Flight < Flights2.FlightWHERE abs(Flights1.Passengers – Flights2. Passengers) <=50AND abs(Flights2.Passengers – Flights3. Passengers) <=50AND abs(Flights1. Passengers – Flights3. Passengers) <=50ORDER BY Flights1. Flight, Flights2. Flight, Flights3. Flight; SQL – queries (Lect. 3)

  26. Limited number of rows • often we want to return only first krows(based on ORDER BY) • slowversion (SQL 92) • SELECT ... FROM tab1 AS aWHERE ( SELECT COUNT(*)FROM tab1 AS bWHERE a.<ordering attribute><b.<ordering attribute>) < k; • fastversion(SQL:1999 non-core, implementsOracle, DB2) • SELECT...FROM ( SELECTROW_NUMBER() OVER (ORDER BY<ordering attribute(s)>ASC| DESC) ASrownumber FROMtablename) WHERErownumber <= k • proprietaryimplementation (nonstandard) • SELECT TOPk ... FROM ... ORDER BY<ordering attribute(s)>ASC| DESC • MS SQL Server • SELECT ... FROM ... ORDER BY<ordering attribute(s)>ASC| DESC LIMITk • MySQL, PostgreSQL SQL – queries (Lect. 3)

  27. Example (MS-SQL notation) What is the largest plane? SELECT TOP 1 PlaneFROMPlanesORDER BYCapacityDESC What are the two largest companies (w.r.t. sums of their passengers)? SELECT TOP2 Company, SUM(Passengers) ASNumberFROMFlightsGROUP BYCompanyORDER BYNumberDESC SQL – queries (Lect. 3)

More Related