Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL SELECT Database Systems Lecture 7 Natasha Alechina.

Similar presentations


Presentation on theme: "SQL SELECT Database Systems Lecture 7 Natasha Alechina."— Presentation transcript:

1 SQL SELECT Database Systems Lecture 7 Natasha Alechina

2 In this Lecture SQL SELECT WHERE clauses SELECT from multiple tables JOINs For more information Connolly and Begg Chapter 5 Ullman and Widom Chapter

3 SQL SELECT Overview SELECT [DISTINCT | ALL] FROM [WHERE ] [ORDER BY ] [GROUP BY ] [HAVING ] ([] - optional, | - or)

4 Example Tables Student IDFirstLast S103JohnSmith S104MaryJones S105JaneBrown S106MarkJones S107JohnBrown Course CodeTitle DBSDatabase Systems PR1Programming 1 PR2Programming 2 IAIIntro to AI Grade IDCodeMark S103DBS72 S103IAI58 S104PR168 S104IAI65 S106PR243 S107PR176 S107PR260 S107IAI35

5 DISTINCT and ALL Sometimes you end up with duplicate entries Using DISTINCT removes duplicates Using ALL retains them - this is the default SELECT ALL Last FROM Student SELECT DISTINCT Last FROM Student Last Smith Jones Brown Jones Brown Last Smith Jones Brown

6 WHERE Clauses Usually you don’t want all the rows A WHERE clause restricts the rows that are returned It takes the form of a condition - only those rows that satisfy the condition are returned Example conditions: Mark < 40 First = ‘John’ First <> ‘John’ First = Last (First = ‘John’) AND (Last = ‘Smith’) (Mark 70)

7 WHERE Examples SELECT * FROM Grade WHERE Mark >= 60 SELECT DISTINCT ID FROM Grade WHERE Mark >= 60 IDCodeMark S103DBS72 S104PR168 S104IAI65 S107PR176 S107PR260 ID S103 S104 S107

8 WHERE Example Given the table Write an SQL query to find a list of the ID numbers and marks in IAI of students who have passed (scored 40 or higher) IAI Grade IDCodeMark S103DBS72 S103IAI58 S104PR168 S104IAI65 S106PR243 S107PR176 S107PR260 S107IAI35 IDMark S10358 S10465

9 One Solution SELECT ID, Mark FROM Grade WHERE (Code = ‘IAI’) AND (Mark >= 40) We only want the ID and Mark, not the Code We’re only interested in IAI Single quotes around the string We’re looking for entries with pass marks

10 SELECT from Multiple Tables Often you need to combine information from two or more tables You can get the effect of a product by using SELECT * FROM Table1, Table2... If the tables have columns with the same name ambiguity results You resolve this by referencing columns with the table name TableName.Column

11 Student IDFirstLast S103JohnSmith S104MaryJones S105JaneBrown S106MarkJones S107JohnBrown SELECT from Multiple Tables SELECT First, Last, Mark FROM Student, Grade WHERE (Student.ID = Grade.ID) AND (Mark >= 40) Grade IDCodeMark S103DBS72 S103IAI58 S104PR168 S104IAI65 S106PR243 S107PR176 S107PR260 S107IAI35

12 And then with the second… and so on Are matched with the first entry from the Student table... All of the entries from the Grade table SELECT from Multiple Tables IDFirstLastIDCodeMark S103JohnSmithS103DBS72 S103JohnSmithS103IAI58 S103JohnSmithS104PR168 S103JohnSmithS104IAI65 S103JohnSmithS106PR243 S103JohnSmithS107PR176 S103JohnSmithS107PR260 S103JohnSmithS107IAI35 S104MaryJonesS103DBS72 S104Mary Jones S103IAI58 S104Mary Jones S104PR168 S104Mary Jones S104IAI65 S104Mary Jones S106PR243 SELECT... FROM Student, Grade WHERE...

13 Grade.IDStudent.ID SELECT from Multiple Tables IDFirstLastIDCodeMark S103JohnSmithS103DBS72 S103JohnSmithS103IAI58 S104Mary Jones S104PR168 S104Mary Jones S104IAI65 S106Mark Jones S106PR243 S107JohnBrownS107PR176 S107JohnBrownS107PR260 S107JohnBrownS107IAI35 SELECT... FROM Student, Grade WHERE (Student.ID = Grade.ID) AND...

14 SELECT from Multiple Tables IDFirstLastIDCodeMark S103JohnSmithS103DBS72 S103JohnSmithS103IAI58 S104Mary Jones S104PR168 S104Mary Jones S104IAI65 S106Mark Jones S106PR243 S107JohnBrownS107PR176 S107JohnBrownS107PR260 SELECT... FROM Student, Grade WHERE (Student.ID = Grade.ID) AND (Mark >= 40)

15 SELECT from Multiple Tables FirstLastMark JohnSmith72 JohnSmith58 Mary Jones 68 Mary Jones 65 Mark Jones 43 JohnBrown76 JohnBrown60 SELECT First, Last, Mark FROM Student, Grade WHERE (Student.ID = Grade.ID) AND (Mark >= 40)

16 SELECT from Multiple Tables When selecting from multiple tables you almost always use a WHERE clause to find entries with common values SELECT * FROM Student, Grade, Course WHERE Student.ID = Grade.ID AND Course.Code = Grade.Code

17 SELECT from Multiple Tables IDFirstLastIDCodeMarkCodeTitle S103JohnSmithS103DBS72DBSDatabase Systems S103JohnSmithS103IAI58IAIIntro to AI S104MaryJonesS104PR168PR1Programming 1 S104MaryJonesS104IAI65IAIIntro to AI S106MarkJonesS106PR243PR2Programming 2 S107JohnBrownS107PR176PR1Programming 1 S107JohnBrownS107PR260PR2Programming 2 S107JohnBrownS107IAI35IAIIntro to AI Student Grade Course Student.ID = Grade.IDCourse.Code = Grade.Code

18 JOINs JOINs can be used to combine tables There are many types of JOIN CROSS JOIN INNER JOIN NATURAL JOIN OUTER JOIN OUTER JOIN s are linked with NULL s - more later A CROSS JOIN B returns all pairs of rows from A and B A NATURAL JOIN B returns pairs of rows with common values for identically named columns and without duplicating columns A INNER JOIN B returns pairs of rows satisfying a condition

19 CROSS JOIN SELECT * FROM Student CROSS JOIN Enrolment IDCode 123DBS 124PRG 124DBS 126PRG Student IDName 123John 124Mary 125Mark 126Jane IDNameIDCode 123John123DBS 124Mary123DBS 125Mark123DBS 126Jane123DBS 123John124PRG 124Mary124PRG 125Mark124PRG 126Jane124PRG 123John124DBS 124Mary124DBS

20 NATURAL JOIN SELECT * FROM Student NATURAL JOIN Enrolment Enrolment IDCode 123DBS 124PRG 124DBS 126PRG Student IDName 123John 124Mary 125Mark 126Jane IDName 123John 124Mary 126Jane Code DBS PRG DBS PRG

21 CROSS and NATURAL JOIN SELECT * FROM A CROSS JOIN B is the same as SELECT * FROM A, B SELECT * FROM A NATURAL JOIN B is the same as SELECT A.col1,… A.coln, [and all other columns apart from B.col1,…B.coln] FROM A, B WHERE A.col1 = B.col1 AND A.col2 = B.col2...AND A.coln = B.col.n (this assumes that col1… coln in A and B have common names)

22 INNER JOIN INNER JOIN s specify a condition which the pairs of rows satisfy SELECT * FROM A INNER JOIN B ON Can also use SELECT * FROM A INNER JOIN B USING (col1, col2,…) Chooses rows where the given columns are equal

23 INNER JOIN SELECT * FROM Student INNER JOIN Enrolment USING (ID) Enrolment IDCode 123DBS 124PRG 124DBS 126PRG Student IDName 123John 124Mary 125Mark 126Jane IDNameIDCode 123John123DBS 124Mary124PRG 124Mary124DBS 126Jane126PRG

24 INNER JOIN SELECT * FROM Buyer INNER JOIN Property ON Price <= Budget Property AddressPrice 15 High St 85, Queen St125, Oak Row175,000 Buyer NameBudget Smith100,000 Jones150,000 Green 80,000 NameBudget AddressPrice Smith100, High St 85,000 Jones150, High St 85,000 Jones150, Queen St125,000

25 INNER JOIN SELECT * FROM A INNER JOIN B ON is the same as SELECT * FROM A, B WHERE SELECT * FROM A INNER JOIN B USING(col1, col2,...) is the same as SELECT * FROM A, B WHERE A.col1 = B.col1 AND A.col2 = B.col2 AND...

26 JOINs vs WHERE Clauses JOINs (so far) are not needed You can have the same effect by selecting from multiple tables with an appropriate WHERE clause So should you use JOINs or not? Yes, because They often lead to concise queries NATURAL JOINs are very common No, because Support for JOINs varies a fair bit among SQL dialects

27 Writing Queries When writing queries There are often many ways to write the query You should worry about being correct, clear, and concise in that order Don’t worry about being clever or efficient Most DBMSs have query optimisers These take a user’s query and figure out how to efficiently execute it A simple query is easier to optimise We’ll look at some ways to improve efficiency later

28 This Lecture in Exams Track cIDNum TitleTimeaID 11 Violent Every Girl Breather Part of Me Star Teaboy4172 CD cIDTitle Price 1Mix Compilation Artist aID Name 1 Stellar 2 Cloudboy

29 This Lecture in Exams Find a list of all the CD titles. (1 mark) Find a list of the titles of tracks that are more than 300 seconds long. (2 marks) Find a list of the names of those artists who have a track on the CD with the title “Compilation”. (4 marks)

30 Next Lecture More SQL SELECT Aliases ‘Self-joins’ Subqueries IN, EXISTS, ANY, ALL For more information Connolly and Begg Chapter 5 Ullman and Widom Chapter 6


Download ppt "SQL SELECT Database Systems Lecture 7 Natasha Alechina."

Similar presentations


Ads by Google