SQL conventions Writing SQL Statements: conventions vs. requirements We try to write SQL statements so they are readable. That is why we try to put SELECT,

Slides:



Advertisements
Similar presentations
SQL/PL SQL Oracle By Rana Umer. Quiz 2 Q1.Create a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City.
Advertisements

Sometimes you need to use data from more than one table. In example1, the report displays data from two separate tables. Employee IDs exist in the EMPLOYEES.
Chapter 4 Joining Multiple Tables
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Displaying Data from Multiple Tables
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
1 DDL – subquery Sen Zhang. 2 Objectives What is a subquery? Learn how to create nested SQL queries Read sample scripts and book for different kinds of.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Upcoming Work Homework #3 due today by 4:30 Homework #4 due Wed Nov 22. Midterm #2 on Wed Nov 29. It will be open book/note (no computers) If you get a.
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
SQL Basics Based on the relational algebra we just learned. Nonprocedural language – what to be done not how Simple, powerful language Used for both data.
Another Join? Find videoIds and storeIds for all copies of Annie Hall which aren’t reserved. We can do a join of movie and video on movieId where title.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
Displaying Data from Multiple Tables. Obtaining Data from Multiple Tables Sometimes you need to use data from more than one table. In the example, the.
Objectives After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
IFS180 Intro. to Data Management Chapter 9 – Outer Joins.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
SQL Joins Oracle and ANSI Standard SQL Lecture 6.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Banner and the SQL Select Statement: Part Three (Joins) Mark Holliday Department of Mathematics and Computer Science Western Carolina University 4 November.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
1 Intro to JOINs SQL INNER JOIN SQL OUTER JOIN SQL FULL JOIN SQL CROSS JOIN Intro to VIEWs Simple VIEWs Considerations about VIEWs VIEWs as filters ALTER.
JOI/1 Data Manipulation - Joins Objectives –To learn how to join several tables together to produce output Contents –Extending a Select to retrieve data.
Week 10 Quiz 9 Answers Group 28 Christine Hallstrom Deena Phadnis.
Chapter 4 Multiple-Table Queries
Recap of SQL Lab no 8 Advance Database Management System.
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Techniques for Manipulating Relational Data By Herbert A. Evans.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Copyright © 2004, Oracle. All rights reserved. D ISPLAYING D ATA FROM M ULTIPLE T ABLES.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
1 Principles of Database Systems With Internet and Java Applications Today’s Topic Chapter 7: SQL, the Structured Query Language Instructor’s name and.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Lecture 8 – SQL Joins – assemble new views from existing tables INNER JOIN’s The Cartesian Product Theta Joins and Equi-joins Self Joins Natural Join.
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
CSED421 Database Systems Lab Join. Human Resources (HR) schema.
SQL Select Statement IST359.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
+ Complex SQL Week 9. + Today’s Objectives TOP GROUP BY JOIN Inner vs. Outer Right vs. Left.
SQL advanced select using Oracle 1 Multiple Tables: Joins and Set Operations Subqueries: Nested Queries.
Quiz Which of the following is not a mandatory characteristic of a relation? Rows are not ordered (Not required) Each row is a unique There is a.
CS 122: Lecture 3 Joins (Part 1) Tarik Booker CS 122 California State University, Los Angeles October 7, 2014.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
4 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
DQL Statements Lab - 3 COMP 353 Summer
SQL – join.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables Using Joins
Contents Preface I Introduction Lesson Objectives I-2
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Presentation transcript:

SQL conventions Writing SQL Statements: conventions vs. requirements We try to write SQL statements so they are readable. That is why we try to put SELECT, FROM, WHERE on newlines. Caps are not required for keywords. Table aliases are usually just a single letter. Fully qualified column names are required if ambiguous. Access will complain. SELECT * FROM Employee e INNER JOIN Timecard t ON e.ssn=t.ssn; is the same as: SELECT * FROM Employee e INNER JOIN Timecard t ON e.ssn=t.ssn;

More SQL (equi-)joins An equi-join is a join where the constraint is on two columns matching (or being equal). A natural join is an equi-join when the columns share the same name in both tables. Both are also called “normal joins” and the INNER JOIN keywords are used for both. But as seen you could also use a WHERE clause. This is by far the most common type of join used.

Inner Join A regular INNER JOIN in with SQL can be written as follows: SELECT * FROM Employee e, Timecard t WHERE e.ssn=t.ssn; or SELECT * FROM Employee e INNER JOIN Timecard t ON e.ssn=t.ssn; e.ssnlastNamefirstNamet.ssndatestartTimeendTimestoreIdpaid UnoJane /14/20028:15:00 AM12:00:00 PM3Yes UnoJane /16/20028:15:00 AM12:00:00 PM3Yes ToulouseJie /14/20028:15:00 AM12:00:00 PM3Yes ThreatAyisha /3/200210:00:00 AM2:00:00 PM5Yes ThreatAyisha /23/20022:00:00 PM10:00:00 PM5Yes ThreatAyisha /21/20023:00:00 PM7:00:00 PM5Yes ThreatAyisha /3/20023:00:00 PM7:00:00 PM3Yes

Why use INNER JOIN SELECT COUNT(*) AS [Count of Cartesian] FROM Employee, Timecard; Count of Cartesian 35 With the INNER JOIN command an ON clause is required so you won’t accidentally get a cross- product instead of a join. This is very likely going to happen to you at some point if you don’t use INNER JOIN.

Other types of joins Self join is a table joined with itself. Recall lab 4 (office). An employee had a mentor. To get all the employees and their mentors we’d use a self-join SELECT e.SSN, e.FNAME, e.LNAME, e.MENTOR, m.FNAME FROM EMPLOYEE AS e INNER JOIN EMPLOYEE AS m ON e.MENTOR=m.SSN; SSNFNAMELNAMEMENTOR BillHomer JoeSmith XenaCreek SSNe.FNAMELNAMEMENTORm.FNAME BillHomer Joe XenaCreek Joe

Outer joins Notice that we only got 2 results. Why? Because Joe does not have a mentor. That is, the MENTOR field for Joe is NULL. Suppose we want to list all the employees, including those that don’t have a mentor. That is what an outer join is for. Rows in one table without matching rows in the other table can be included in the results. LEFT OUTER JOIN includes those in the left without matches RIGHT OUTER JOIN includes those in the right without matches FULL OUTER JOIN (both, but not in Access -> use union instead)

LEFT OUTER self-join SELECT e.SSN, e.FNAME, e.LNAME, e.MENTOR, m.FNAME FROM EMPLOYEE AS e LEFT JOIN EMPLOYEE AS m ON e.MENTOR=m.SSN; SSNe.FNAMELNAMEMENTORm.FNAME JoeSmith BillHomer Joe XenaCreek Joe

RIGHT OUTER self-join SELECT e.SSN, e.FNAME, e.LNAME, e.MENTOR, m.FNAME FROM EMPLOYEE AS e RIGHT JOIN EMPLOYEE AS m ON e.MENTOR=m.SSN; SSNe.FNAMELNAMEMENTORm.FNAME Bill BillHomer Joe XenaCreek Joe Xena What the heck does this mean?

Cross Product e.SSNe.FNAMEe.LNAMEe.MENTORm.SSNm.FNAMEm.LNAMEm.MENTOR JoeSmith JoeSmith BillHomer JoeSmith XenaCreek JoeSmith JoeSmith BillHomer BillHomer BillHomer XenaCreek BillHomer JoeSmith XenaCreek BillHomer XenaCreek XenaCreek XenaCreek SELECT * FROM EMPLOYEE e, EMPLOYEE m;

ssnlastNamefirstName UnoJane ToulouseJie ThreatAyisha FortuneJulian FivozinskyBruce EMPLOYEE ssndatestartTimeendTimestoreIdpaid /14/20028:15:00 AM12:00:00 PM3Yes /16/20028:15:00 AM12:00:00 PM3Yes /14/20028:15:00 AM12:00:00 PM3Yes /3/200210:00:00 AM2:00:00 PM5Yes /3/20023:00:00 PM7:00:00 PM3Yes /23/20022:00:00 PM10:00:00 PM5Yes /21/20023:00:00 PM7:00:00 PM5Yes TIMECARD storeIdstreetcitystatezipcodemanager Liberty Rd.ApopkaFL N. Monroe St.ApopkaFL STORE

Multi-table joins Let’s look at all the timecards for employees. SELECT e.firstName, e.lastName, t.date, t.startTime FROM Timecard t INNER JOIN Employee e ON t.ssn=e.ssn; firstNamelastNamedatestartTime JaneUno1/14/20028:15:00 AM JaneUno1/16/20028:15:00 AM JieToulouse1/14/20028:15:00 AM AyishaThreat1/3/200210:00:00 AM AyishaThreat2/23/20022:00:00 PM AyishaThreat3/21/20023:00:00 PM AyishaThreat1/3/20023:00:00 PM

3 table join SELECT e.firstName, e.lastName, t.date, t.startTime, s.street AS [Store Street], s.city AS [Store City] FROM Store s INNER JOIN (Timecard t INNER JOIN Employee e ON t.ssn=e.ssn) ON t.storeId=s.storeId; firstNamelastNamedatestartTimeStore StreetStore City JaneUno1/14/20028:15:00 AM2010 Liberty Rd.Apopka JaneUno1/16/20028:15:00 AM2010 Liberty Rd.Apopka JieToulouse1/14/20028:15:00 AM2010 Liberty Rd.Apopka AyishaThreat1/3/200210:00:00 AM1004 N. Monroe St.Apopka AyishaThreat2/23/20022:00:00 PM1004 N. Monroe St.Apopka AyishaThreat3/21/20023:00:00 PM1004 N. Monroe St.Apopka AyishaThreat1/3/20023:00:00 PM2010 Liberty Rd.Apopka

Arrangement of joins What happens if I rearrange the joins? SELECT e.firstName, e.lastName, t.date, t.startTime, s.street AS [Store Street], s.city AS [Store City] FROM Employee e INNER JOIN (Timecard t INNER JOIN Store s ON t.storeId=s.storeId) ON e.ssn=t.ssn; firstNamelastNamedatestartTimeStore StreetStore City JaneUno1/14/20028:15:00 AM2010 Liberty Rd.Apopka JaneUno1/16/20028:15:00 AM2010 Liberty Rd.Apopka JieToulouse1/14/20028:15:00 AM2010 Liberty Rd.Apopka AyishaThreat1/3/200210:00:00 AM1004 N. Monroe St.Apopka AyishaThreat2/23/20022:00:00 PM1004 N. Monroe St.Apopka AyishaThreat3/21/20023:00:00 PM1004 N. Monroe St.Apopka AyishaThreat1/3/20023:00:00 PM2010 Liberty Rd.Apopka

Non-equi (theta) joins We are not limited to doing equi-joins on foreign key – primary key columns. You can also do joins based on comparison operators >, >=, These are rare. SELECT s.storeId, w.ssn FROM Store s INNER JOIN WorksIn w ON s.storeId <> w.storeId; storeIdssn