Presentation is loading. Please wait.

Presentation is loading. Please wait.

CM2020: Introduction to Database Systems Queries In Relational Databases Database Systems 4 th edition Connolly and Begg Chapter 5 Dr Nirmalie Wiratunga.

Similar presentations


Presentation on theme: "CM2020: Introduction to Database Systems Queries In Relational Databases Database Systems 4 th edition Connolly and Begg Chapter 5 Dr Nirmalie Wiratunga."— Presentation transcript:

1 CM2020: Introduction to Database Systems Queries In Relational Databases Database Systems 4 th edition Connolly and Begg Chapter 5 Dr Nirmalie Wiratunga nw@comp.rgu.ac.uk

2 Tutorials and Labs Jointly organised with Dr Robin Boswell (rab@comp.rgu.ac.uk) and Hatem Ahriz (ha@comp.rgu.ac.uk) Tutorials: Tuesdays 2-3pm –A (Robin): C39/39a SAS (CS2, CIM2) –B (Nirmalie): C411 Schoolhill (BIS2, MMD3, CIMD3) –C (Hatem):B40 SAS (ISTD2, ISTD3, CGA2) Labs: Tuesday 3-5pm –A (Nirmalie): C18 SAS (CS2, CIM2) –B (Hatem): C24 SAS (BIS2, MMD3, CIMD3) –C (Robin): C26 St Andrew street (ISTD2, ISTD3, CGA2)

3 Last week … student# name address age course 100 Bobby Dundee 17 C100 200 Helen Aberdeen 21 C100 300 Helen Stirling 18 C200 400 Freddy Perth 30 C300 course# course_name location C100 Computing St Andrews St C200 Pharmacy Schoolhill C300 Architecture Garthdee Relational Databases (DB)s Consists of relations or tables Tables consists of rows and columns Primary Key uniquely identifies each row Composite primary keys are allowed and formed by combining multiple columns Foreign key is an attribute / set of attributes, within one relation that matches the primary key of another relation Table: Students Table: Courses

4 SQL Structured Query Language (SQL) Data Manipulation Language –SELECT Single table Multi table –MS Access (Query by Example) –GROUP BY with SELECT –INSERT, UPDATE, DELETE Data Definition Language –CREATE TABLE, DROP TABLE, ALTER TABLE

5 Querying allows retrieval of data in response to a question about the data in the DB Relational DBMSs translates queries into SQL (a.k.a Structured Query Language or SEQUEL) SQLs SELECT construct –extracts data from the tables of the database –allows specification of conditions –does not facilitate modification to the tables or the data contained in tables Querying with SELECT

6 SELECT SELECT {* | [column1 [AS newName]] [,column2, …]} [ALL | DISTINCT] FROM table1 [alias] [, table2,…] [WHERE "conditions"] [GROUP BY "column-list"] [HAVING "conditions"] [ORDER BY "column-list" [ASC | DESC] ]; Everything within [ ] is optional Standard syntax is: ; denotes end of SELECT statement

7 Single-Table Select Queries Drivers driver# name address points D010 FredPerth7 D020 Jimmy Dundee4 D030 DavidDundee2 D040 JohnStirling3 D050 BobbyAberdeen12 The result of a query is another table - this table is not permanent - this table is called a DYNASET (a.k.a Dynamic Data Set) in Microsoft Access

8 SELECT Drivers driver# name address points D010 Fred Perth 7 D020 Jimmy Dundee 4 D030 David Dundee 2 D040 John Stirling 3 D050 Bobby Aberdeen 12 EXAMPLE: List all the ids (driver#) from the Drivers table. SELECT driver# FROM Drivers; driver# D010 D020 D030 D040 D050 SELECT driver#

9 SELECT cont Drivers driver# name address points D010 Fred Perth 7 D020 Jimmy Dundee 4 D030 David Dundee 2 D040 John Stirling 3 D050 Bobby Aberdeen 12 EXAMPLE: List all the ids (driver#) and points from the Drivers table SELECT driver#, points FROM Drivers; driver# points D010 7 D020 4 D030 2 D040 3 D050 12 SELECT driver#, points We use commas to separate fields

10 SELECT with Wildcards SELECT * FROM Drivers; Drivers driver# name address points D010 Fred Perth 7 D020 Jimmy Dundee 4 D030 David Dundee 2 D040 John Stirling 3 D050 Bobby Aberdeen 12 driver# name address points D010 Fred Perth 7 D020 Jimmy Dundee 4 D030 David Dundee 2 D040 John Stirling 3 D050 Bobby Aberdeen 12 EXAMPLE: List all the details (i.e. all the fields) from the Drivers table SELECT * We use * for all fields

11 SELECT with WHERE Drivers driver# name address points D010 Fred Perth 7 D020 Jimmy Dundee 4 D030 David Dundee 2 D040 John Stirling 3 D050 Bobby Aberdeen 12 EXAMPLE: List all the ids (driver#) of the drivers from Dundee from the Drivers table SELECT driver# FROM Drivers WHERE address = Dundee; driver# D020 D030 SELECT driver# WHERE address = Dundee;

12 Extending SELECT We can extend SELECT in a number of ways: 1. Can pattern match within certain fields of a table - can use the LIKE clause 2. Can check if a field has a value from a set of values - can use the IN operator 3. Can perform calculations on values in a table - can use COLUMN FUNCTIONS 4. Can make your queries have a choice or be more specific - can use the OR and AND clauses 5. Can sort output of a query into ascending/descending order - can use the ORDER BY clause

13 The LIKE clause Sometimes we may want to pattern match within certain fields of a table ?matches a single characterLike Sm?th *matches any number of characters Like St* #matches a single digit [0-9]Like D0#0 [list]matches any character in listLike [A-F]* [!list]matches any character not in list Like [!A-F]*

14 SELECT LIKE Drivers driver# name address points D010 Fred Perth 7 D020 Jimmy Dundee 4 D030 David Dundee 2 D040 John Stirling 3 D050 Bobby Aberdeen 12 EXAMPLE: List all the names and addresses of the drivers whose address begins with the letters Du from the Drivers table SELECT name, address FROM Drivers WHERE address Like Du*; name address jimmy Dundee David Dundee SELECT name, address WHERE address Like Du*;

15 The IN Operator Drivers driver# name address points D010 Fred Perth 7 D020 Jimmy Dundee 4 D030 David Dundee 2 D040 John Stirling 3 D050 Bobby Aberdeen 12 The IN operator is used to check if an attribute(s) has a value from a set of values [NOT] IN ( ) name Jimmy David Bobby Syntax is: a) List the names of drivers from Dundee or Aberdeen SELECT name FROM Drivers WHERE address IN (Dundee, Aberdeen); b) List the names of drivers not from Dundee or Aberdeen SELECT name FROM Drivers WHERE address NOT IN (Dundee, Aberdeen); name Fred John

16 Column / Aggregate Functions Column functions perform calculations on the values in the column of a table COUNT - calculates the number of values in a column SUM - calculates the sum (total) of all values in a column AVG - calculates the average of all values in a column MAX - calculates the maximum value in a column MIN - calculates the minimum value in a column

17 SELECT with Aggregates Drivers driver# name address points D010 Fred Perth 7 D020 Jimmy Dundee 4 D030 David Dundee 2 D040 John Stirling 3 D050 Bobby Aberdeen 12 EXAMPLE: Calculate the number of drivers, the total number of points, the average number of points, the maximum points value and the range of points from the Driver SELECT COUNT(driver#), SUM(points), AVG(points), MAX(points), MIN(points), MAX(points) - MIN(points) FROM Drivers; COUNT(driver#) SUM(points) AVG(points) MAX(points) MIN(points) MAX(points) - MIN(points) 528 5.6 12 2 10

18 SELECT with ORDER BY Drivers driver# name address points D010 Fred Perth 7 D020 Bobby Dundee 4 D030 David Dundee2 D040 John Stirling 3 D050 Jimmy Aberdeen 12 List in alphabetical order, names of drivers with a Dundee or Aberdeen address SELECT name FROM Drivers WHERE address IN (Dundee, Aberdeen) ORDER BY name; name Bobby David Jimmy NOTE: Default is ascending SELECT name IN (Dundee, Aberdeen);

19 SELECT with ORDER BY List in reverse alphabetical order, names of drivers with a Dundee or Aberdeen address SELECT name FROM Drivers WHERE address IN (Dundee, Aberdeen) ORDER BY name DESC; name Jimmy David Bobby Drivers driver# name address points D010 Fred Perth 7 D020 Bobby Dundee 4 D030 David Dundee2 D040 John Stirling 3 D050 Jimmy Aberdeen 12 IN (Dundee, Aberdeen); SELECT name DESC means descending

20 Complex Conditions with WHERE Mathematical operators –=,, <>, = Logical operators AND, OR, NOT

21 Combining Operators AND Drivers driver# name address points D010 Fred Perth7 D020 Jimmy Dundee 4 D030 David Dundee 2 D040 John Stirling 3 D050 Bobby Aberdeen 12 EXAMPLE: List the names of drivers whose licence points are between 4 and 15 from the Drivers table and not from Dundee SELECT name FROM Drivers WHERE points >= 4 AND points <= 15 AND (address <> Dundee'); name Fred Bobby SELECT name WHERE points >= 4 AND points <= 15 AND (address <> Dundee);

22 Drivers driver# name address points D010 Fred Perth7 D020 Jimmy Dundee 4 D030 David Dundee 2 D040 John Stirling 3 D050 Bobby Aberdeen 12 EXAMPLE: List the names of drivers with addresses from either Aberdeen or Dundee SELECT name FROM Drivers WHERE (address LIKE D*') OR (address LIKE A*'); name Jimmy David Bobby SELECT name WHERE (address LIKE D*) OR (address LIKE A*) Combining Operators OR

23 Mathematical Operators supports the following –+, -, *, / (division), % (modulo) item# qtyprice Shoes220 Raft160 Skateboard1020 Life Vest15 SELECT item, qty*price AS TotalCost FROM Items_ordered; item# TotalCost Shoes40 Raft60 Skateboard200 Life Vest5 Items_ordered Use AS to specify new column name

24 Summary: Single Table Queries Queries may be created in a DBMS using SQL SELECT statement The basic ANSI syntax is: SELECT FROM [ WHERE ] ; Can pattern match within certain fields of a table - LIKE clause Can check if a field has a value from a set of values - IN operator Can perform calculations on values in a table - COLUMN FUNCTIONS Can make your queries have a choice or be more specific - OR and AND clauses Can sort output of a query into ascending/descending order - ORDER BY clause

25 Multi-Table Queries Commonly require data from more than one table Involves JOINING tables to form a SUPERTABLE Table JOINS –PRODUCT or CARTESIAN –INNER JOIN or EQUI JOIN –NATURAL JOIN –OUTER JOIN (LEFT and RIGHT)

26 Multi-Table Select Queries List data on all orders including customer names and addresses Customers cust# name address C100AllanAberdeen C101JohnDundee C102BettyStirling C200 Dean Dundee Orders order# cust# date 2000 C100 20/11/01 3000 C101 27/11/01 4000 C100 30/11/01

27 Product Join forms every possible combination of rows combining all columns E.g Customer PRODUCT Order C.cust# nameaddress order# O.cust# date C100AllanAberdeen 2000 C100 20/11/01 C101JohnDundee 2000 C100 20/11/01 C102BettyStirling 2000 C100 20/11/01 C200 Dean Dundee 2000 C100 20/11/01 C100AllanAberdeen 3000 C101 27/11/95 etc SELECT * FROM Customer, Order;

28 Inner Join (or Equi-Join) 1.Apply a product join 2.Retrieve rows with matching linked attribute values E.g Customer INNER JOIN Order C.cust# name address order# O.cust# date C100 AllanAberdeen 2000 C100 20/11/01 C101 John Dundee 3000 C101 27/11/01 C100 AllanAberdeen 4000 C100 30/11/01 SELECT * FROM Customer C, Order O WHERE C.cust# = O.cust#; Inner join is a more useful kind of join for data retrieval Use of alias. Can also use AS here. E.g. Order AS O

29 Natural Join C.cust# name address order# date C100 AllanAberdeen 2000 20/11/01 C101 John Dundee 3000 27/11/01 C100 AllanAberdeen 4000 30/11/01 SELECT C.cust, name, address, order#, date FROM Customer C, Order O WHERE C.cust# = O.cust#; Similar to Inner join but with a single matching attribute e.g. no O.cust#

30 Queries on joined tables cust# name address order# date C101 John Dundee 3000 27/11/01 C100 Allan Aberdeen 400030/11/01 SELECT C.cust#, name, order#, date FROM Customer AS C, Order AS O WHERE Customer.cust# = Orders.cust# AND date > 25/11/01;

31 OUTER JOIN Use when we want to see the details from unmatched rows as well –columns of the output corresponding to unavailable data are set to null Right Outer JOIN –E.g. Not all customers have placed orders in the Orders table Left Outer Join

32 rows in the 1 st table with unmatched rows in the 2 nd table are still included with NULL values E.g Customer LEFT OUTER JOIN Order cust#nameaddress order# date C100AllanAberdeen 2000 20/11/01 C101JohnDundee 3000 27/11/01 C100 AllanAberdeen 4000 30/11/01 C102BettyStirling NULL NULL C200Dean Dundee NULL NULL SELECT Customer.cust#, name, order#, date FROM Customer LEFT JOIN order ON Customer.cust# = Orders.cust#;

33 Summary Multi-Table Joins It is possible to join as many tables as you want –generally for n tables n-1 joins Inner Join (and Natural join) is most common –outer joins will display unmatched rows

34 Query Processor Extracts required information from database How? –Structured Query Language (SQL) –Query by Example (QBE) Querying without having to know SQL

35 QBE with MS Access E.g. Inner Join

36 MS Access SQL View Functionally Similar

37 SQL - Next Structured Query Language (SQL) Data Manipulation Language –SELECT Single table Multi table –MS Access (Query by Example) –GROUP BY with SELECT –INSERT, UPDATE, DELETE Data Definition Language –CREATE TABLE, DROP TABLE, ALTER TABLE

38 Grouping Results Useful to summarise results Closely integrated with SELECT –Specify column names –Aggregate functions e.g. count, avg, sum, max, min –WHERE conditions –Any combination of the above All columns names in the SELECT must appear in the GROUP BY –HAVING clause used to specify conditions on groups of rows

39 GROUP BY ANSI syntax is: All select columns Must appear here Specify conditions applicable to groups SELECT {* | [column1 [AS newName]] [,column2, …]} [ALL | DISTINCT] FROM table1 [alias] [, table2,…] [WHERE "conditions"] [GROUP BY "column-list"] [HAVING "conditions"] [ORDER BY "column-list" [ASC | DESC] ];

40 Group By Clause Example Given the following table: Employees NI_no name addresssalary NS 111111 Fred Aberdeen15,000 NS 222222 Bobby Dundee20,000 NS 333333 Dave Aberdeen12,000 NS 444444 Steve Stirling10,000 NS 555555 Betty Dundee25,000 Show, for each address, the number of employees that live there SELECT address, count(NI_no) FROM Employees GROUP BY address; Address count(NI_no) Aberdeen2 Dundee2 Stirling1

41 Group By Clause Example In order to do the previous query, an intermediate table is created that is group by (sorted on) address i.e.: Employees NI_no nameaddress salary NS 111111 FredAberdeen 15,000 NS 333333 DaveAberdeen 12,000 NS 222222 BobbyDundee 20,000 NS 555555 BettyDundee 25,000 NS 444444 SteveStirling 10,000 We perform count(NI_no) for each of the 3 groupings grouping 1 grouping 2 grouping 3

42 Given the following table: Employees NI_no name addresssalary NS 111111 Fred Aberdeen15,000 NS 222222 Bobby Dundee20,000 NS 333333 Dave Aberdeen12,000 NS 444444 Steve Stirling10,000 NS 555555 Betty Dundee25,000 Show the addresses whose employees average salary is greater than £21,000. Also show the average salary. SELECT address, avg(salary) FROM Employees GROUP BY address HAVING avg(salary) > 12000; address avg(salary) Aberdeen 13500 Dundee 22500 NOTE: compare with intermediate table from previous slide GROUP BY with Having


Download ppt "CM2020: Introduction to Database Systems Queries In Relational Databases Database Systems 4 th edition Connolly and Begg Chapter 5 Dr Nirmalie Wiratunga."

Similar presentations


Ads by Google