SQL: Structured Query Language

Slides:



Advertisements
Similar presentations
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
Advertisements

1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
E-R Diagram for a Banking Enterprise
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Dec 4, 2003Murali Mani SQL B term 2004: lecture 14.
©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.
SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Query by example (based on notes by Silberchatz,Korth, and Sudarshan.
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
CS3431 SQL : Query Language. CS3431 SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”  (sName=“Greg” AND address=“320 FL”)
SQL Neyha Amar CS 157A, Fall Inserting The insert statement is used to add a row of data into a table Strings should be enclosed in single quotes,
CMSC424: Database Design Instructor: Amol Deshpande
Murali Mani SQL. Murali Mani SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”  (sName=“Greg” AND address=“320 FL”) (Student)
--The SQL Query Language DML--1 LIKE  LIKE allows to select character strings which have some element in common by using wild cards:  Wild cards:  “%”
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
MySQL Tutorial (2) Introduction to Database. Banking Example branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-city)
Instructor: Mohamed Eltabakh
CIS552SQL1 Data Definition Language Insertions Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations.
SQL I. SQL – Introduction  Standard DML/DDL for relational DB’s  DML = “Data Manipulation Language” (queries, updates)  DDL = “Data Definition Language”
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
Chapter 3 SQL. 2 Relational Commercial Languages  SQL (Structured Query Language)  the standard relational DB language  used interactively which transforms.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE) Datalog.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
Basic Structure Given sets A 1, A 2,..., A n a relation r is a subset of A 1 x A 2 x... x A n Thus a relation is a set of n-tuples (a 1, a 2,..., a n )
Structured Query Language 2 Presented by: Annisa, M.Kom. Source: Database System Concepts 5 th edition.
Relational Algebra Instructor: Mohamed Eltabakh 1.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE)
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Copyright © Curt Hill Queries in SQL More options.
Advanced SQL: Triggers & Assertions
Computing & Information Sciences Kansas State University Monday, 17 Sep 2007CIS 560: Database System Concepts Lecture 12 of 42 Monday, 18 February 2008.
Source: Database System Concepts, Silberschatz etc Edited: Wei-Pang Yang, IM.NDHU, Introduction to Database CHAPTER 5 Other Relational Languages.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Relational Algebra Instructor: Mohamed Eltabakh 1 Part II.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Relational Algebra Instructor: Mohamed Eltabakh 1.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
1 Schema for Student Registration System Student Student (Id, Name, Addr, Status) Professor Professor (Id, Name, DeptId) Course Course (DeptId, CrsCode,
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1 Part II.
Relational Algebra Instructor: Mohamed Eltabakh 1.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1.
Structured Query Language
Structured Query Language (Data Manipulation Language)
SQL Structured Query Language 11/9/2018 Introduction to Databases.
SQL : Query Language Part II CS3431.
SQL Views CS542.
SQL : Query Language CS3431.
Functional Dependencies and Normalization
SQL Views and Updates cs3431.
Functional Dependencies and Normalization
Instructor: Mohamed Eltabakh
Instructor: Mohamed Eltabakh
SQL – Entire Select.
Chapter 4 Summary Query.
Instructor: Mohamed Eltabakh
Access: SQL Participation Project
SQL: Structured Query Language
Instructor: Mohamed Eltabakh
SQL: Structured Query Language
Introduction to Database
SQL: Structured Query Language
SQL: Structured Query Language
Advanced Topics: Indexes & Transactions
Presentation transcript:

SQL: Structured Query Language Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu

Cartesian Product in SQL In Relation Algebra: R x S In SQL, add R and S to FROM clause No WHERE condition that links R and S SELECT * FROM Student, Professor; SELECT sName, pNumber FROM Student, Professor;

Cross Product - Example Student Professor sNumber sName address professor 1 Dave 320FL 2 Greg 3 Matt pNumber pName addr 1 MM 141FL 2 ER 201FL SELECT * FROM Student, Professor; sNumber sName address professor pNumber pName addr 1 Dave 320FL MM 141FL 2 ER 201FL Greg 3 Matt

Theta Join in SQL In Relation Algebra: R ⋈C S In SQL, add R and S to FROM clause WHERE condition that links R and S with the join condition C SELECT * FROM Student, Professor WHERE Student.pNum = Professor.Number; Join condition

Theta Join Example Student Professor SELECT sNumber, sName, pName address profNum 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 141FL 2 ER 201FL SELECT sNumber, sName, pName FROM Student, Professor WHERE profNum = pNumber; sNumber sName pName 1 Dave MM 2 Greg 3 Matt ER  sNumber,sName,pName(Student ⋈(profNum=pNumber) Professor)

Theta Join Example Student Professor sNumber sName address profNum 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 141FL 2 ER 201FL If column names are the same  use relationName.attrName SELECT sName, pName, S.address FROM Student S, Professor P WHERE S.address = P.address;  sName,pName,S.address(ρS(Student) ⋈(S.address=P.address) ρP(Professor))

Natural Join Student ⋈ Professor Reminder: Join columns must have same names in both relations (R ⋈ S) Student ⋈ Professor SELECT * FROM Student NATURAL JOIN Professor; SELECT * FROM Student , Professor WHERE Student.pnumber = Professor.pnumber ; Explicitly add the equality join condition

Difference between the two Queries below Common columns will appear once Student ⋈ Professor SELECT * FROM Student NATURAL JOIN Professor; Common columns will appear twice SELECT * FROM Student , Professor WHERE Student.pnumber = Professor.pnumber ; Explicitly add the equality join condition

Natural Join - Example Student ⋈ Professor Professor Student Must be the same name Student Professor sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt pNumber pName addr 1 MM 141FL 2 ER 201FL SELECT * FROM Student natural join Professor; Common cols appear once sNumber sName address pNumber pName addr 1 Dave 320FL MM 141FL 2 Greg 3 Matt ER 201FL

Theta Join - Example Student ⋈Student.pNumber = Professor.pNumber Professor Student Professor sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt pNumber pName addr 1 MM 141FL 2 ER 201FL SELECT * FROM Student S, Professor P Where S.pNumber = P.pNumber; All columns will appear sNumber sName address S.pNumber P.pNumber pName addr 1 Dave 320FL MM 141FL 2 Greg 3 Matt ER 201FL

Sorting: ORDER BY clause New optional clause that you can add to the SELECT statement called “ORDER BY” Allows sorting the returned records according to one or more fields SELECT * FROM Student WHERE sNumber >= 1 ORDER BY pNumber, sName; Default is ascending order SELECT * FROM Student WHERE sNumber >= 1 ORDER BY pNumber ASC, sName DESC; -Order first based on the pNumber (ascending) -If many records exist with the same pNumber - order them based on sName (descending)

Sorting: ORDER BY clause SELECT * FROM Student WHERE sNumber >= 1 ORDER BY pNumber, sName; Order By does not change the content of the output relation. It only change the order of tuples Must be the last clause in the SELECT statement.

Sorting: ORDER BY clause Student sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt SELECT * FROM Student WHERE sNumber >= 1 ORDER BY pNumber, sName DESC; sNumber sName address pNumber 2 Greg 320FL 1 Dave 3 Matt  (pNumber, sName DESC) ( (sNumber >= 1) (Student))

Duplicate Elimination in SQL New optional keyword “DISTINCT” Added in the SELECT clause SELECT DISTINCT … FROM … … Eliminate any duplicates from the answer

Duplicate Elimination: Example Student sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER (sName,address(Student)) ( (address) ( (sNumber > 1) (Student))) SELECT DISTINCT sName, address FROM Student; SELECT DISTINCT address FROM Student WHERE sNumber > 1; sName address Dave 320FL Greg Matt address 320FL

X Always Remember…. Only SELECT and FROM clauses are mandatory All the others are optional You can mix and match the optional ones But if you add a clause, then keep it in its order SELECT DISTINCT address FROM Student WHERE sNumber > 1; SELECT address FROM Student ORDER BY sNumber; SELECT address FROM Student WHERE sNumber > 1 ORDER BY sNumber; SELECT address FROM Student ORDER BY sNumber WHERE sNumber > 1; X

Example Queries DBMS is smart enough !!! (Select first, then joins) SELECT customer_name FROM borrower B, loan L WHERE B.loan_number = L.loan_number AND L.branch_name = “Perryridge”; Tested second Tested first

Example Queries Find customer names having account balance below 100 or above 10,000 SELECT customer_name FROM account A, depositor D WHERE A.account_number = D.account_number AND balance < 100 OR balance >10,000; It is better to write the condition than using Natural Join These two conditions will execute first before the join.

Example Queries For branches that gave loans > 100,000 or hold accounts with balances >50,000, report the branch name along whether it is reported because of a loan or an account SELECT branch_name, ‘Loan’ As Type FROM Loan WHERE amount > 100,000 Union SELECT branch_name, ‘Account’ As Type FROM account WHERE balance > 50,000

Aggregation + GroupBy

Possible Aggregations in SQL SELECT COUNT (*) FROM Student; SELECT COUNT (sNumber) FROM Student; SELECT MIN (sNumber) FROM Student; SELECT MAX (sNumber) FROM Student; SELECT SUM (sNumber) FROM Student; SELECT AVG (sNumber) FROM Student;

Grouping & Aggregation in SQL New optional clause called “GROUP BY” If the SELECT statement has “WHERE” Then WHERE conditions are evaluated first, then records are grouped Then count the records in each group And get the minimum gpa for each group SELECT pNumber, COUNT (sName), Min(gpa) FROM Student GROUP BY pNumber; First form groups for each pNumber

GROUP BY: Example I Student cnt  count(*) (Student) sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt 4 Jan 500MA cnt  count(*) (Student) pNumber,cnt count(*) ( (sNumber > 1) (Student)) SELECT count(*) AS CNT FROM Student; SELECT pNumber, count(*) AS CNT FROM Student WHERE sNumber > 1 GROUP BY pNumber; CNT 4 pNumber CNT 1 2

GROUP BY: Example II Student sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt 4 Jan 500MA pNumber,address, CNT  count(sName), SUM  sum(sNumber) ( (sNumber > 1) (Student)) SELECT pNumber,address, count(sName) AS CNT, sum(sNumber) AS SUM FROM Student WHERE sNumber >= 1 GROUP BY pNumber, address; pNumber address CNT SUM 1 320FL 2 3 500MA 4