More SQL: Complex Queries,

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
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)
Introduction to SQL J.-S. Chou Assistant Professor.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Chapter 3 Single-Table Queries
Relational Query Languages. Languages of DBMS  Data Definition Language DDL  define the schema and storage stored in a Data Dictionary  Data Manipulation.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Advanced SQL Murat Kantarcioglu Adapted from Silberchatz et al. slides.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
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 © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
NULL VALUES CHAPTER 5 (6/E) CHAPTER 8 (5/E) 1. LECTURE OUTLINE  Dealing with null values Three-valued logic Effects in WHERE clauses IS NULL Effects.
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
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.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe CHAPTER 7 More SQL: Complex Queries, Triggers, Views, and Schema Modification Slide 7- 1.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 10 July 10,2012 More SQL: Complex Queries, Triggers,
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CSE202 Database Management Systems
CS580 Advanced Database Topics
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Chapter 4 Basic SQL.
6/22/2018.
CS580 Advanced Database Topics
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CS 405G: Introduction to Database Systems
More SQL: Complex Queries, Triggers, Views, and Schema Modification
The Relational Algebra and Relational Calculus
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CSCE 315 – Programming Studio Spring 2010 Project 1, Lecture 4
Chapter 7 Introduction to Structured Query Language (SQL)
SQL (more).
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Contents Preface I Introduction Lesson Objectives I-2
CSC 453 Database Systems Lecture
SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir
CSC 453 Database Systems Lecture
Presentation transcript:

More SQL: Complex Queries,

Outline More Complex SQL Retrieval Queries

More Complex SQL Retrieval Queries Additional features allow users to specify more complex retrievals from database: Nested queries, joined tables, outer joins, aggregate functions, and grouping

Comparisons Involving NULL and Three-Valued Logic Meanings of NULL Unknown value Unavailable or withheld value Not applicable attribute Each individual NULL value considered to be different from every other NULL value SQL uses a three-valued logic: TRUE, FALSE, and UNKNOWN

Comparisons Involving NULL and Three-Valued Logic (cont’d.)

Comparisons Involving NULL and Three-Valued Logic (cont’d.) SQL allows queries that check whether an attribute value is NULL IS or IS NOT NULL

Nested Queries, Tuples, and Set/Multiset Comparisons Complete select-from-where blocks within WHERE clause of another query Outer query Comparison operator IN Compares value v with a set (or multiset) of values V Evaluates to TRUE if v is one of the elements in V

Nested Queries (cont’d.)

Nested Queries (cont’d.) Use tuples of values in comparisons Place them within parentheses

Nested Queries (cont’d.) Use other comparison operators to compare a single value v = ANY (or = SOME) operator Returns TRUE if the value v is equal to some value in the set V and is hence equivalent to IN Other operators that can be combined with ANY (or SOME): >, >=, <, <=, and <>

Nested Queries (cont’d.) Avoid potential errors and ambiguities Create tuple variables (aliases) for all tables referenced in SQL query

Correlated Nested Queries Correlated nested query Evaluated once for each tuple in the outer query

The EXISTS and UNIQUE Functions in SQL EXISTS function Check whether the result of a correlated nested query is empty or not EXISTS and NOT EXISTS Typically used in conjunction with a correlated nested query SQL function UNIQUE(Q) Returns TRUE if there are no duplicate tuples in the result of query Q

Explicit Sets and Renaming of Attributes in SQL Can use explicit set of values in WHERE clause

Explicit Sets and Renaming of Attributes in SQL

Joined Tables in SQL and Outer Joins Permits users to specify a table resulting from a join operation in the FROM clause of a query The FROM clause in Q1A Contains a single joined table

Joined Tables in SQL and Outer Joins (cont’d.) Specify different types of join NATURAL JOIN Various types of OUTER JOIN NATURAL JOIN on two relations R and S No join condition specified Implicit EQUIJOIN condition for each pair of attributes with same name from R and S

Joined Tables in SQL and Outer Joins (cont’d.) Inner join Default type of join in a joined table Tuple is included in the result only if a matching tuple exists in the other relation LEFT OUTER JOIN Every tuple in left table must appear in result If no matching tuple Padded with NULL values for attributes of right table

Joined Tables in SQL and Outer Joins (cont’d.) RIGHT OUTER JOIN Every tuple in right table must appear in result If no matching tuple Padded with NULL values for the attributes of left table FULL OUTER JOIN Can nest join specifications

Aggregate Functions in SQL Used to summarize information from multiple tuples into a single-tuple summary Grouping Create subgroups of tuples before summarizing Built-in aggregate functions COUNT, SUM, MAX, MIN, and AVG Functions can be used in the SELECT clause or in a HAVING clause

Aggregate Functions in SQL (cont’d.) NULL values discarded when aggregate functions are applied to a particular column

Grouping: The GROUP BY and HAVING Clauses Partition relation into subsets of tuples Based on grouping attribute(s) Apply function to each such group independently GROUP BY clause Specifies grouping attributes If NULLs exist in grouping attribute Separate group created for all tuples with a NULL value in grouping attribute

Grouping: The GROUP BY and HAVING Clauses (cont’d.) Provides a condition on the summary information

Discussion and Summary of SQL Queries

The DROP Command DROP command Drop behavior options: Example: Used to drop named schema elements, such as tables, domains, or constraint Drop behavior options: CASCADE and RESTRICT Example: DROP SCHEMA COMPANY CASCADE;

The ALTER Command Alter table actions include: Example: Adding or dropping a column (attribute) Changing a column definition Adding or dropping table constraints Example: ALTER TABLE COMPANY.EMPLOYEE ADD COLUMN Job VARCHAR(12); To drop a column Choose either CASCADE or RESTRICT

The ALTER Command (cont’d.) Change constraints specified on a table Add or drop a named constraint

Summary Complex SQL: Nested queries, joined tables, outer joins, aggregate functions, grouping