Database Management Systems

Slides:



Advertisements
Similar presentations
CSCI3170 Introduction to Database Systems
Advertisements

TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Relational Algebra Dashiell Fryer. What is Relational Algebra? Relational algebra is a procedural query language. Relational algebra is a procedural query.
Ver 1,12/09/2012Kode :CCs 111,Sistem basis DataFASILKOM Chapter 3: SQL Bambang Irawan Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
E-R Diagram for a Banking Enterprise
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Ordering the Display of Tuples List in alphabetic order the names of all customers having.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
1 Creating a Non-Conditional List A- What are you going to do? You will “list” “all of the records” in a database. (it means you will not use any condition!)
Structured Query Language Part I Chapter Three CIS 218.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
SQL Exercises1 Revising RDB and SQL CTEC2902 Advanced Programming.
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
SQL By: Toan Nguyen. Download Download the software at During the installation –Skip sign up for fast installation.
Computer Science 101 Web Access to Databases SQL – Extended Form.
SQL Operations Aggregate Functions Having Clause Database Access Layer A2 Teacher Up skilling LECTURE 5.
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
BACS 287 Structured Query Language 1. BACS 287 Visual Basic Table Access Visual Basic provides 2 mechanisms to access data in tables: – Record-at-a-time.
Copyright © Curt Hill Queries in SQL More options.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL – Simple Queries and JOIN MGMT 360 Database Management.
Advanced Relational Algebra & SQL (Part1 )
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
More Relation Operations 2014, Fall Pusan National University Ki-Joune Li.
SQL SELECT Getting Data from the Database. Basic Format SELECT, FROM WHERE (=, >, LIKE, IN) ORDER BY ; SELECT LastName, FirstName, Phone, City FROM Customer.
Relational Algebra p BIT DBMS II.
Single-Table Queries 2: Advanced Topics CS 320. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data SELECT field1, field2,
SQL: Single Table Queries SELECT FROM WHERE ORDER D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 1.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts - 6 th Edition Recursive Queries.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
INFORMATION TECHNOLOGY DATABASE MANAGEMENT. A database is a collection of information organized to provide efficient retrieval. The collected information.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
SQL Query Getting to the data ……..
CC La Web de Datos Primavera 2017 Lecture 7: SPARQL [i]
Relational Database Design
Module 2: Intro to Relational Model
Writing Basic SQL SELECT Statements
02 | Advanced SELECT Statements
Chapter 3 Introduction to SQL(3)
Using the Set Operators
Database Management Systems
CS 440 Database Management Systems
Chapter 2: Intro to Relational Model
Sorting and Filtering Data
Using the Set Operators
Intro to PostgreSQL.
GROUP BY & Subset Data Analysis
Chapter 4 Summary Query.
Database systems Lecture 3 – SQL + CRUD
Access: SQL Participation Project
SQL Fundamentals in Three Hours
CS4222 Principles of Database System
One-Pass Algorithms for Database Operations (15.2)
Chapter 2: Intro to Relational Model
Contents Preface I Introduction Lesson Objectives I-2
Chapter 2: Intro to Relational Model
CSC 453 Database Systems Lecture
Section 4 - Sorting/Functions
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
Using the Set Operators
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

Database Management Systems Faculty of Computer Science Technion – Israel Institute of Technology Database Management Systems Course 236363 Tutorial 4: SQL Queries

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

Database example Customers(Cust_Id, Cust_Name, Faculty) Cust_Id: Customer ID (unique) Cust_Name: Customer Name Faculty: Faculty Name

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

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

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

Database example Ordered(Cust_Id, Book_Id, Order_Date) Cust_Id: Customer ID Book_Id : Book ID Order_Date: Date of book order

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

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

Database example Borrowed(Book_Id, Cust_Id, From_Date, To_Date) 13-Oct-2002 56789 5555

Outline

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 }] ...]

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

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

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

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

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

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

SQL Queries: SET Operations UNION query1 UNION [ALL] query2 INTERSECT query1 INTERSECT [ALL] query2 EXCEPT query1 EXCEPT [ALL] query2 Keep duplicates

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

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

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

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

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

SQL Queries: SET Operations SELECT Cust_Id from orderd UNION SELECT from_date from borrowed Problem – date and ID are of different types

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

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

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

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

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: https://www.postgresql.org/docs/9.6/static/functions-array.html

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;

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]