More SQL Select Database Systems Lecture 8 Natasha Alechina.

Slides:



Advertisements
Similar presentations
More SQL Data Definition
Advertisements

Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
Yet More SQL SELECT Database Systems Lecture 9 Natasha Alechina.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Subqueries and Set Operations.
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Database Management Systems & Programming Faculty of Information & Media Studies Summer 2000 LIS Week 6 Structured Query Language.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Subqueries and Set Operations.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
Security and Integrity
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 8: Subqueries.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Missing Information Database Systems Lecture 10 Natasha Alechina.
Physical DB Issues, Indexes, Query Optimisation Database Systems Lecture 13 Natasha Alechina.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
Oracle Database Administration Lecture 2 SQL language.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
Lecture8:Data Manipulation in SQL Advanced SQL queries Ref. Chapter5 Lecture8 1.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
SQL (DDL & DML Commands)
Nested Queries (Sub Queries) A nested query is a form of a SELECT command that appears inside another SQL statement. It is also termed as subquery. The.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
1 JOIN SUBQUERY Structured Query Language (SQL) - Part III.
Structured Query Language (SQL) Ask and ye shall receive. The Bible.
1 Querying a Single Table Structured Query Language (SQL) - Part II.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Chapter 12 Subqueries and Merge Statements
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
CSCI N311: Oracle Database Programming 4-1 Chapter 12: When One Query Depends Upon Another Correlated Subqueries. Subqueries in the where clause. Anti-Joins.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
1 Introduction to SQL Database Systems. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation.
Subqueries.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational State Assertions These slides.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.
Dr Gordon Russell, Napier University Unit SQL 3 - V2.0 1 SQL 3 Unit 1.4.
Using Subqueries to Solve Queries
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Introduction to Database Systems, CS420
Using the Set Operators
Using Subqueries to Solve Queries
Writing Correlated Subqueries
More SQL Nested and Union queries, and more
CS 405G: Introduction to Database Systems
الفصل الثاني الصيغة العامة لجمله select*
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Aggregating Data Using Group Functions
Using Subqueries to Solve Queries
Reporting Aggregated Data Using the Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Using the Set Operators
Reporting Aggregated Data Using the Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
SELECT from Multiple Tables
Instructor: Zhe He Department of Computer Science
Aggregating Data Using Group Functions
Presentation transcript:

More SQL Select Database Systems Lecture 8 Natasha Alechina

More SQL SELECT In This Lecture More SQL Select Aliases ‘Self-joins’ Subqueries IN, EXISTS, ANY, ALL For more information Connoly and Begg Chapter 5 Ullman and Widom Chapter 6.3.

More SQL SELECT But first… 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

More SQL SELECT Exercise Find a list of the names of those artists who have a track on the CD with the title “Compilation”. (Several versions are on ). SELECT Name FROM Artist, Track, CD WHERE (Artist.aID = Track.aID) AND (Track.cID) = CD.cID) AND (CD.Title = ‘Compilation’)

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

More SQL SELECT This ‘AS’ is optional, but Oracle doesn’t accept it at all Aliases Aliases rename columns or tables to Make names more meaningful Make names shorter and easier to type Resolve ambiguous names Two forms: Column alias SELECT column AS newName... Table alias SELECT... FROM table AS newName

More SQL SELECT Example SELECT E.ID AS empID, E.Name, W.Dept FROM Employee E WorksIn W WHERE E.ID = W.ID Employee IDName 123John 124Mary WorksIn IDDept 123Marketing 124Sales 124Marketing

More SQL SELECT Example SELECT E.ID AS empID, E.Name, W.Dept FROM Employee E WorksIn W WHERE E.ID = W.ID empID Name Dept 123 John Marketing 124 Mary Sales 124 Mary Marketing

More SQL SELECT Aliases and ‘Self-Joins’ Aliases can be used to copy a table, so that it can be combined with itself: SELECT A.Name FROM Employee A, Employee B WHERE A.Dept=B.Dept AND B.Name=‘Andy’ Employee Name Dept John Marketing Mary Sales Peter Sales Andy Marketing Anne Marketing

More SQL SELECT Aliases and Self-Joins B Name Dept John Marketing Mary Sales Peter Sales Andy Marketing Anne Marketing A Name Dept John Marketing Mary Sales Peter Sales Andy Marketing Anne Marketing Employee AEmployee B

More SQL SELECT Aliases and Self-Joins A.Name A.DeptB.Name B.Dept John MarketingJohn Marketing Mary SalesJohn Marketing Peter SalesJohn Marketing Andy MarketingJohn Marketing Anne MarketingJohn Marketing John MarketingMary Sales Mary SalesMary Sales Peter SalesMary Sales Andy MarketingMary Sales Anne MarketingMary Sales SELECT … FROM Employee A, Employee B …

More SQL SELECT Aliases and Self-Joins A.Name A.DeptB.Name B.Dept John MarketingJohn Marketing Andy MarketingJohn Marketing Anne MarketingJohn Marketing Mary SalesMary Sales Peter SalesMary Sales Mary SalesPeter Sales Peter SalesPeter Sales John MarketingAndy Marketing Andy MarketingAndy Marketing Anne MarketingAndy Marketing SELECT … FROM Employee A, Employee B WHERE A.Dept = B.Dept

More SQL SELECT Aliases and Self-Joins A.Name A.DeptB.Name B.Dept John MarketingAndy Marketing Andy MarketingAndy Marketing Anne MarketingAndy Marketing SELECT … FROM Employee A, Employee B WHERE A.Dept = B.Dept AND B.Name = ‘Andy’

More SQL SELECT Aliases and Self-Joins SELECT A.Name FROM Employee A, Employee B WHERE A.Dept = B.Dept AND B.Name = ‘Andy’ A.Name John Andy Anne The result is the names of all employees who work in the same department as Andy.

More SQL SELECT Subqueries A SELECT statement can be nested inside another query to form a subquery The results of the subquery are passed back to the containing query E.g. get the names of people who are in Andy’s department: SELECT Name FROM Employee WHERE Dept = (SELECT Dept FROM Employee WHERE Name=‘Andy’)

More SQL SELECT Subqueries SELECT Name FROM Employee WHERE Dept = (SELECT Dept FROM Employee WHERE Name=‘Andy’) First the subquery is evaluated, returning the value ‘Marketing’ This result is passed to the main query SELECT Name FROM Employee WHERE Dept = ‘Marketing’

More SQL SELECT Subqueries Often a subquery will return a set of values rather than a single value You can’t directly compare a single value to a set Options IN - checks to see if a value is in the set EXISTS - checks to see if the set is empty or not ALL/ANY - checks to see if a relationship holds for every/one member of the set

More SQL SELECT (NOT) IN Using IN we can see if a given value is in a set of values NOT IN checks to see if a given value is not in the set The set can be given explicitly or from a subquery SELECT FROM WHERE IN SELECT FROM WHERE NOT IN

More SQL SELECT (NOT) IN Employee NameDepartmentManager JohnMarketingChris MaryMarketingChris ChrisMarketingJane PeterSalesJane JaneManagement NameDepartmentManager JohnMarketingChris MaryMarketingChris ChrisMarketingJane PeterSalesJane SELECT * FROM Employee WHERE Department IN (‘Marketing’, ‘Sales’)

More SQL SELECT (NOT) IN Employee NameDepartmentManager JohnMarketingChris MaryMarketingChris ChrisMarketingJane PeterSalesJane JaneManagement SELECT * FROM Employee WHERE Name NOT IN (SELECT Manager FROM Employee)

More SQL SELECT (NOT) IN First the subquery SELECT Manager FROM Employee is evaluated giving This gives SELECT * FROM Employee WHERE Name NOT IN (‘Chris’, ‘Jane’) Manager Chris Jane NameDepartmentManager JohnMarketingChris MaryMarketingChris PeterSalesJane

More SQL SELECT (NOT) EXISTS Using EXISTS we see if there is at least one element in a set NOT EXISTS is true if the set is empty The set is always given by a subquery SELECT FROM WHERE EXISTS SELECT FROM WHERE NOT EXISTS

More SQL SELECT (NOT) EXISTS Employee NameDepartmentManager JohnMarketingChris MaryMarketingChris ChrisMarketingJane PeterSalesJane JaneManagement NameDepartmentManager ChrisMarketingJane JaneManagement SELECT * FROM Employee E1 WHERE EXISTS ( SELECT * FROM Employee E2 WHERE E2.Name = E1.Manager)

More SQL SELECT ANY and ALL ANY and ALL compare a single value to a set of values They are used with comparison operators like =, >,, >=, <= val = ANY (set) is true if there is at least one member of the set equal to the value val = ALL (set) is true if all members of the set are equal to the value

More SQL SELECT ALL Find the names of the employee(s) who earn the highest salary SELECT Name FROM Employee WHERE Salary >= ALL ( SELECT Salary FROM Employee) NameSalary Mary20,000 John15,000 Jane25,000 Paul30,000

More SQL SELECT ANY Find the names of employee(s) who earn more than someone else SELECT Name FROM Employee WHERE Salary > ANY ( SELECT Salary FROM Employee) NameSalary Mary20,000 John15,000 Jane25,000 Paul30,000

More SQL SELECT Word Searches Commonly used for searching product catalogues etc. Want to be able to search by keyword Want to be able to use word stemming for flexible searching For example: given a database of books, Searching for “crypt” would return “Cryptonomicon” by Neil Stephenson “Applied Cryptography” by Bruce Schneier

More SQL SELECT Word Searches To do a word search we can keep A table of items to be searched A table of keywords A linking table saying which keywords belong to which items Items itmIDitmTitle Keywords keyIDkeyWord ItemKey itmIDkeyID

More SQL SELECT Word Searches To search we can use queries like SELECT * FROM Items WHERE itmID IN ( SELECT itmID FROM ItemKey WHERE keyID IN ( SELECT keyID FROM Keywords WHERE keyWord LIKE 'crypt%‘))

More SQL SELECT Word Searches Sometimes you need to search for a set of words To find entries with all words you can link conditions with AND To find entries with any of the words use OR SELECT * FROM Items WHERE itmID IN ( SELECT itmID FROM ItemKey WHERE keyID IN ( SELECT keyID FROM Keywords WHERE keyWord LIKE 'word1%')) AND itmID IN ( SELECT itmID FROM ItemKey WHERE keyID IN ( SELECT keyID FROM Keywords WHERE keyWord LIKE 'word2%'))

More SQL SELECT Next Lecture Yet more SQL ORDER BY Aggregate functions GROUP BY and HAVING UNION etc. For more information Connoly and Begg Chapter 5 Ullman and Widom Chapter 6.4