Download presentation
Presentation is loading. Please wait.
1
Database Management Systems
Faculty of Computer Science Technion – Israel Institute of Technology Database Management Systems Course Tutorial 4: SQL Queries
2
Database example Library Database Ordered Cust_Id Book_Id Order_Date
Books Book_Id Book_Name Year Max_Time Faculty Pages Customers Cust_Id Cust_Name Faculty Borrowed Cust_Id Book_Id From_Date To_Date
3
Database example Customers(Cust_Id, Cust_Name, Faculty)
Cust_Id: Customer ID (unique) Cust_Name: Customer Name Faculty: Faculty Name
4
Database example Customers(Cust_Id, Cust_Name, Faculty) Faculty
CS Moshe Cohen 12345 EE Avi Barak 23456 MED 34567 Lior Edri 45678 56789 67890 Two Avi Barak, different faculties Two Moishe Cohen in EE
5
Database example Books(Book_Id, Book_Name, Year, Max_Time, Faculty, Pages) Book_Id: Unique book id Book_Name: Bool title Year: Year of print Max_Time: Maximum borrowing time in days Faculty: Faculty name Pages: Page number
6
Database And Knowledge
Database example Books(Book_Id, Book_Name, Year, Max_Time, Faculty, Pages) Faculty Pages Max_Time Year Book_Name Book_Id CS 348 7 1998 Database Systems 1111 14 1112 424 2001 1113 390 1 Database And Knowledge 2222 EE 2223 180 21 Electronic Circuits 3333 MED 580 1985 Genes 7 4444 450 1988 Anatomy 5555
7
Database example Ordered(Cust_Id, Book_Id, Order_Date)
Cust_Id: Customer ID Book_Id : Book ID Order_Date: Date of book order
8
Database example Ordered(Cust_Id, Book_Id, Order_Date) Order_Date
14-Oct-2002 1111 12345 24-Oct-2002 1112 45678 30-Oct-2002 1113 12-Oct-2002 2222
9
Database example Borrowed(Book_Id, Cust_Id, From_Date, To_Date)
Book_Id: Book ID Cust_Id: Customer ID From_Date: Borrowing date To_Date: Return date
10
Database example Borrowed(Book_Id, Cust_Id, From_Date, To_Date)
13-Oct-2002 56789 5555
11
Outline
12
SQL Queries: Sort SELECT select_list FROM table_expression ORDER BY sort_expression1 [ASC | DESC] [NULLS { FIRST | LAST }] [, sort_expression2 [ASC | DESC] [NULLS { FIRST | LAST }] ...]
13
SQL Queries: Sort Return the costumers names and ids ordered by
customer name (in ascending order) and by ids in descending order SELECT cust_name, cust_id FROM customers ORDER BY cust_name, cust_id DESC
14
SQL Queries: Sort Customers(Cust_Id, Cust_Name, Faculty) Faculty
CS Moshe Cohen 12345 EE Avi Barak 23456 MED 34567 Lior Edri 45678 56789 67890 Two Avi Barak, different faculties Two Moishe Cohen in EE
15
SQL Queries: Sort SELECT cust_name, cust_id FROM customers ORDER BY cust_name, cust_id DESC Cust_Name Cust_Id Avi Barak 34567 23456 Lior Edri 45678 Moshe Cohen 67890 56789 12345
16
SQL Queries: LIMIT & OFFSET
SELECT select_list FROM table_expression [ ORDER BY ... ] [ LIMIT { number | ALL } ] [ OFFSET number ] Must use ORDER BY while using LIMIT/OFFSET Allows to return a range of sorted result The indices are determined by the ORDER BY
17
SQL Queries: LIMIT & OFFSET
Return the 3rd and 4th customers from previous query SELECT cust_name, cust_id FROM customers ORDER BY cust_name, cust_id DESC
18
SQL Queries: LIMIT & OFFSET
cust_name cust_id Avi Barak 34567 23456 Lior Edri 45678 Moshe Cohen 67890 56789 12345 SELECT cust_name, cust_id FROM customers ORDER BY cust_name, cust_id DESC OFFSET 2 LIMIT 2 cust_name cust_id Lior Edri 45678 Moshe Cohen 67890
19
SQL Queries: SET Operations
UNION query1 UNION [ALL] query2 INTERSECT query1 INTERSECT [ALL] query2 EXCEPT query1 EXCEPT [ALL] query2 Keep duplicates
20
SQL Queries: SET Operations
Recall A table is a bag of tuples Not a set since the same tuple can appear several times We can specify how to handle duplicates: To keep them we write the operation name + ALL To eliminate we just write the operation name To perform set operations schemas must be type-equal
21
SQL Queries: SET Operations
Example: Return the customers id that preformed an action in the library (borrowed or ordered a book) SELECT Cust_Id from orderd UNION SELECT Cust_Id from borrowed
22
SQL Queries: SET Operations
SELECT Cust_Id from orderd UNION SELECT Cust_Id from borrowed Order_Date Book_Id Cust_Id 14-Oct-2002 1111 12345 24-Oct-2002 1112 45678 30-Oct-2002 1113 12-Oct-2002 2222 To_Date From_Date Cust_Id Book_Id 13-Oct-2002 56789 5555
23
SQL Queries: SET Operations
SELECT Cust_Id from orderd UNION SELECT Cust_Id from borrowed Cust_Id 12345 45678 cust_id 56789 12345 45678 Cust_Id 56789
24
SQL Queries: SET Operations
SELECT Cust_Id from orderd UNION SELECT book_id from borrowed Cust_Id 12345 45678 cust_id 55555 12345 45678 Book_id 55555
25
SQL Queries: SET Operations
SELECT Cust_Id from orderd UNION SELECT from_date from borrowed Problem – date and ID are of different types
26
SQL Queries: WITH WITH creates a temporary table to use within the query you write Unlike views, this temporary table is calculated only as a part of the query, and its definition is not saved on the server. WITH TempCSBooks as ( SELECT Book_Id, Book_Name FROM Books WHERE Faculty = 'CS' ) SELECT * FROM TempCSBooks
27
SQL Queries: WITH RECURSIVE
WITH RECURSIVE allows to preform calculations can not be done with standard SQL syntax Calculations are done until reaches fixpoint The current calculations does not change the result of the previuos one Example: calculate the sum of all the numbers from 1 to 100 WITH RECURSIVE t(n) AS ( VALUES (1) UNION SELECT n+1 FROM t WHERE n < 100 ) SELECT sum(n) FROM t; At each step the input is the ouput of the prev step
28
SQL Queries: WITH RECURSIVE
𝑟 0 𝑟 1 𝑟 2 𝑟 99 N 1 N 2 N 3 … N 100 𝑖 𝑟 𝑖 At each step the input is the ouput of the prev step N 1 2 …. 100
29
SQL Queries: WITH RECURSIVE
given a table of edges in the graph, find all paths without a cycle For a non-fixpoint queries this cannot be done We need to know how much JOIN operations to make Source Destination 1 2 3 4
30
SQL Queries: WITH RECURSIVE
Arrays: Add element \ array concatenation : || [1,2,3] || 4 => ]1,2,3,4] You can use the ANY \ ALL operator to check if a value is in array 4 =ANY [1,2,3,4] More information:
31
SQL Queries: WITH RECURSIVE
WITH RECURSIVE find_paths(source, destination, length, path, cycle) AS ( SELECT source, destination, 1, ARRAY[source], false FROM edges e UNION ALL SELECT fp.source, e.destination, fp.length + 1, path || e.source, e.source = ANY(path) OR e.destination = ANY(path) FROM edges e, find_paths fp WHERE e.source = fp.destination AND NOT cycle ) SELECT * FROM find_paths WHERE NOT cycle order by source, destination, length;
32
SQL Queries: WITH RECURSIVE
source destination length path cycle 1 2 [1] FALSE [1, 3] 3 [1, 3, 4] [1, 2] 4 [1, 3, 2] [1, 2, 3] [2] [2, 3] [3] [3, 4] [3, 2] [4] [4, 2]
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.