Structured Query Language DML MIS 520 – Database Theory Fall 2001 (Day) Lecture 10/11.

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

© Abdou Illia MIS Spring 2014
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
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.
 Database is SQL1.mdb ◦ import using MySQL Migration Toolkit 
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Subqueries and Set Operations.
LECTURE 10.  Group functions operate on sets of rows to give one result per group.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Subqueries and Set Operations.
Sanjay Goel, School of Business, University at Albany, SUNY 1 SQL- Data Manipulation Language ITM 692 Sanjay Goel.
Introduction to Structured Query Language (SQL)
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
--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:  “%”
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 8: Subqueries.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 3 Single-Table Queries
Introduction to Databases Chapter 7: Data Access and Manipulation.
Chapter 9 Joining Data from Multiple Tables
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
Using Special Operators (LIKE and IN)
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
IST 210 SQL Todd Bacastow IST 210: Organization of Data.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Chapter 12 Subqueries and Merge Statements
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Query Processing – Implementing Set Operations and Joins Chap. 19.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Day 5 - More Complexity With Queries Explanation of JOIN & Examples Explanation of JOIN & Examples Explanation & Examples of Aggregation Explanation &
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
There’s a particular style to it… Rob Hatton
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
IFS180 Intro. to Data Management Chapter 10 - Unions.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL Query Getting to the data ……..
More SQL: Complex Queries,
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
References: Text Chapters 8 and 9
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Working with Tables: Join, Functions and Grouping
SQL FUNDAMENTALS CDSE Days 2018.
SQL Structured Query Language 11/9/2018 Introduction to Databases.
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL – Entire Select.
Chapter 4 Summary Query.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Access: SQL Participation Project
Chapter 7 Introduction to Structured Query Language (SQL)
Contents Preface I Introduction Lesson Objectives I-2
CSC 453 Database Systems Lecture
Section 4 - Sorting/Functions
Presentation transcript:

Structured Query Language DML MIS 520 – Database Theory Fall 2001 (Day) Lecture 10/11

SQL – Select Select From Where Group By Having Order By

Conceptual Evaluation From Tables: Cross product and join operations 1 Restriction on where conditions 2 Group By? Order By? Project columns in SELECT finish Sort on Group BY columns Yes Compute aggregates and reduce each group to 1 row 3 4 Restriction on HAVING conditions 5 Sort columns in ORDER BY No Yes 6 7

SQL – DISTINCT Eliminates all the duplicate entries in the table resulting from the query. Syntax: Select [DISTINCT] select_list From table[, table, …] [Where expression] [Order By expression] Example: Select DISTINCT studio_id, director_id From Movies studio_id director_id

SQL – Order By Used to sort the results based on contents of a column Multiple levels of sort can be done by specifying multiple columns An expression can be used in Order By clause Syntax: Select function(column) From table1 [, table2 …] [Where condition] [Order By {Column | alias | position} [ASC | DESC]]

SQL – Order By Example: Sort Movies by profits in Ascending order Select MovieTitle, Gross, Budget, (Gross – Budget) as profits From movies Order BY profits Great Escape Upside Down54504 Green Warrior Blue Oranges28721 Movie_titleGrossBudgetProfit

Aggregate Queries – Group By Categorizes the query results according to the contents of a column in the database Multiple levels of subgroups can be created by specifying multiple columns Syntax: Select column1, [column2, …] From table [, table …] [Where condition] Group By column1, [column2, ….] Having [Condition]

Aggregate Queries – Group By Example: Get # of movies by each director for each studio Select studio_id, director_id, count(*) From Movies Group By director_id, studio_id Example: Get # of movies by each studio ordered by studio_id Select studio_id, count(*) From Movies Group By studio_id Order By studio_id

Aggregate Queries – Group By Example: Select studio_id, Sum(budget) From movies Group by studio_id Having Sum(budget) > 60 Example: Select studio_id, count(*) From Movies Group By studio_id Order By studio_id

Aggregate Queries Aggregate queries provides a more holistic view of the data by further processing the retrieved data. They can work on –On all the rows in a table –A subset of rows in a table selected using a where clause –Groups of selected data organized using Group By clause. Syntax: Select function(column) From Where Group By Having

Aggregate Queries Functions: –Sum()Returns a sum of the column –Count()Returns a total number of rows returned by a query –Avg()Returns the average of a column –Min()Returns minimum value of the column returned by query –Max()Returns maximum value of the column returned by query Notes 1: Count function does not include columns containing null values in total Notes 2: Count can be used with distinct to count the number of distinct rows Example: Query:Select sum(budget) From movies Where studio_id = 3 Output:Sum(budget)

SQL – Join A Join is a Query that combines data from multiple tables –Multiple tables are specified in the From Clause –For two tables to be joined in a sensible manner, they need to have data in common Example: Schema: Movies (movie_title, director_id, release_date) People(person_fname, person_lname, person_id) Query: Select movie_title, person_fname, person_lname From Movies, People Where director_id = person_id

SQL – Joining Condition For a useful Join query a joining condition is required –Defined in where clause as relationships between columns –Multiple conditions may be defined if multiple columns shared –More than two tables can be joined in a query Example: Find people who live in same state as studio Schema: Studios(studio_id, studio_state, studio_name, studio_city) People(person_fname, person_lname, person_id, person_state, person_city) Query: Select person_fname, person_lname, studio_name From Movies, People Where studio_city = person_city AND studio_state = person_state

SQL – More than two tables Example: Get title, director, studio, city for all movies in the database Schema: Studios(studio_id, studio_state, studio_name, studio_city) People(person_fname, person_lname, person_id, person_state, person_city) Movies(movie_title, director_id, studio_id) Query: Select M.movie_title, M.studio_id, P.person_fname, P.person_lname, S.studio_city From Movies M, People P, Studio S Where M.director_id = P.person_id AND M.studio_id = P.person_id

SQL – Self Join Required to compare values within a single column –Need to define aliases for the table names Example: Find actors living in the same state Schema: People(person_fname, person_lname, person_id, person_state, person_city) Query: Select p1.person_id, p1.person_fname, p1.person_lname, p1.person_state From People p1, People p2 Where p1.person_state = p2.person_state AND p1.person_id != p2.person_id Note: Distinct operator is critical because if there are more than two people from any state each person will appear as many times as there are people from the state

SQL-92 – Join More verbose than pervious versions of SQL –Need to define aliases for the table names Separates the condition for joining from condition for filtering Example: Find actors living in the same state Schema: People(person_fname, person_lname, person_id, person_state, person_city) Movies(movie_title, director_id, studio_id) Query: Select movie_title, person_fname, person_lname From Movies INNER JOIN People ON director_id = person_id Select movie_title, person_fname, person_lname From Movies INNER JOIN People ON director_id = person_id Where studio_id = 1

SQL-92 – Multiple Table Join Example: Get title, director, studio, city for all movies in database Schema: Studios(studio_id, studio_state, studio_name, studio_city) People(person_fname, person_lname, person_id, person_state, person_city) Movies(movie_title, director_id, studio_id) Query: Select Movies.movie_title, Movies.studio_id, Person.person_fname, Person.person_lname, Studio.studio_city From (People Inner Join (Movies Inner Join Studio On Studio.studio_id = Movie.studio_id) On Movie.director_id = Person.person_id

SQL-92 – Left/Right Join Example: Schema: People(person_fname, person_lname, person_id, person_state, person_city) Movies(movie_id, movie_title, director_id, studio_id) Location(movie_id, city, state) Query: Select movie_title, city, state From Movies Left Join Locations On Movies.movie_id = Locations.movie_id Select movie_title, person_fname, person_lname From Movies Right Join People On Movies.director_id = Person.person_id Includes all non matched movie titles Includes all people not matching to directors

Nested Queries A sub query is a query nested within another query –The enclosing query also called outer query –Nested query is called inner query There can be multiple levels of nesting Example: Select movie_title From movies Where director_id IN ( Select person_id From People Where person_state = ‘TX’)

Nested Queries - Types Non-Correlated Sub Queries: –Requires data required by outer query before it can be executed –Inner query does not contain any reference to outer query –Behaves like a function Example: People(person_fname, person_lname, person_id, person_state, person_city) Movies(movie_id, movie_title, director_id, studio_id) Select movie_title, studio_id From Movies Where director_id IN ( Select person_id From People Where person_state = ‘TX’) Steps: 1.Subquery is executed 2.Subquery results are plugged into the outer query 3.The outer query is processed

Nested Queries - Types Correlated Sub Queries: –Contains reference to the outer query –Behaves like a loop Example: People(person_fname, person_lname, person_id, person_state, person_city) Cast_Movies(cast_member_id, role, movie_id) Select person_fname, person_lname From People p1 Where ‘Pam Green’ in ( Select role From Cast_Movies Where p1.person_id = cast_member_id ) Steps: –Contents of the table row in outer query are read –Sub-query is executed using data in the row being processed. –Results of the inner query are passed to the where in the outer query –The Outer query is Processed

Equivalent Join Query Example: People(person_fname, person_lname, person_id, person_state, person_city) Cast_Movies(cast_member_id, role, movie_id) Select person_fname, person_lname From People, Cast_Movies Where Cast_member_id = person_id And role = ‘Pam Green’