SQL - Data Query language

Similar documents
COMP 244 DATABASE CONCEPTS & APPLICATIONS

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

CS 582 Database Management Systems II

EECS 647: Introduction to Database Systems

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. General Overview - rel. model. Overview - detailed - SQL

Database Management Systems. Chapter 5

Chapter 3: Introduction to SQL

The SQL data-definition language (DDL) allows defining :

CSEN 501 CSEN501 - Databases I

CSC Web Programming. Introduction to SQL

Computing for Medicine (C4M) Seminar 3: Databases. Michelle Craig Associate Professor, Teaching Stream

Chapter 3: Introduction to SQL

Database Management Systems. Chapter 5

4. SQL - the Relational Database Language Standard 4.3 Data Manipulation Language (DML)

SQL Part 2. Kathleen Durant PhD Northeastern University CS3200 Lesson 6

SQL: Data Manipulation Language. csc343, Introduction to Databases Diane Horton Winter 2017

SQL: csc343, Introduction to Databases Renée J. Miller and Fatemeh Nargesian and Sina Sina Meraji. Winter 2018

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #3: SQL and Rela2onal Algebra- - - Part 1

Lecture 6 - More SQL

Database Usage (and Construction)

SQL - Lecture 3 (Aggregation, etc.)

Ian Kenny. November 28, 2017

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

Goals for Today. CS 133: Databases. Relational Model. Multi-Relation Queries. Reason about the conceptual evaluation of an SQL query

CS3DB3/SE4DB3/SE6DB3 TUTORIAL

SQL. CS 564- Fall ACKs: Dan Suciu, Jignesh Patel, AnHai Doan

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

12. MS Access Tables, Relationships, and Queries

Lesson 2. Data Manipulation Language

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls

Basic form of SQL Queries

CMP-3440 Database Systems

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Set theory is a branch of mathematics that studies sets. Sets are a collection of objects.

Announcements. Relational Model & Algebra. Example. Relational data model. Example. Schema versus instance. Lecture notes

Relational Algebra and SQL

Querying Data with Transact SQL

Chapter 4: SQL. Basic Structure

SQL. Lecture 4 SQL. Basic Structure. The select Clause. The select Clause (Cont.) The select Clause (Cont.) Basic Structure.

CMPT 354: Database System I. Lecture 4. SQL Advanced

COSC 304 Introduction to Database Systems SQL. Dr. Ramon Lawrence University of British Columbia Okanagan

Lecture 3 More SQL. Instructor: Sudeepa Roy. CompSci 516: Database Systems

CMPT 354: Database System I. Lecture 3. SQL Basics

II (The Sequel) We will use the following database as an example throughout this lab, found in students.db.

Information Systems Engineering. SQL Structured Query Language DML Data Manipulation (sub)language

SQL Data Query Language

What s a database system? Review of Basic Database Concepts. Entity-relationship (E/R) diagram. Two important questions. Physical data independence

SQL Data Manipulation Language. Lecture 5. Introduction to SQL language. Last updated: December 10, 2014

SQL: The Query Language Part 1. Relational Query Languages

SQL. SQL Queries. CISC437/637, Lecture #8 Ben Cartere9e. Basic form of a SQL query: 3/17/10

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 8 Advanced SQL

Database Systems SQL SL03

Chapter 3: SQL. Chapter 3: SQL

COMP 244 DATABASE CONCEPTS AND APPLICATIONS

What happens. 376a. Database Design. Execution strategy. Query conversion. Next. Two types of techniques

Chapter 4 SQL. Database Systems p. 121/567

Silberschatz, Korth and Sudarshan See for conditions on re-use

Keys, SQL, and Views CMPSCI 645

Lecture 3 SQL - 2. Today s topic. Recap: Lecture 2. Basic SQL Query. Conceptual Evaluation Strategy 9/3/17. Instructor: Sudeepa Roy

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

Relational Database: The Relational Data Model; Operations on Database Relations

Database Systems SQL SL03

Oracle Database 11g: SQL and PL/SQL Fundamentals

Database Applications (15-415)

Relational Algebra. Procedural language Six basic operators

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

Basic operators: selection, projection, cross product, union, difference,

Introduction to Query Processing and Query Optimization Techniques. Copyright 2011 Ramez Elmasri and Shamkant Navathe

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Administrivia. Administrivia. Faloutsos/Pavlo CMU /615

Chapter 3: SQL. Database System Concepts, 5th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

More on SQL Nested Queries Aggregate operators and Nulls

A Sample Solution to the Midterm Test

Missing Information. We ve assumed every tuple has a value for every attribute. But sometimes information is missing. Two common scenarios:

SQL QUERIES. CS121: Relational Databases Fall 2017 Lecture 5

Principles of Data Management

Introduction to Database Systems CSE 444

Database Languages. A DBMS provides two types of languages: Language for accessing & manipulating the data. Language for defining a database schema

3/3/2008. Announcements. A Table with a View (continued) Fields (Attributes) and Primary Keys. Video. Keys Primary & Foreign Primary/Foreign Key

Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations Views Modification of the Database Data Definition

Querying Data with Transact-SQL

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

Introduction SQL DRL. Parts of SQL. SQL: Structured Query Language Previous name was SEQUEL Standardized query language for relational DBMS:

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

The SELECT-FROM-WHERE Structure

SQL Interview Questions

Relational Database Management Systems for Epidemiologists: SQL Part I

Plan for today. Query Processing/Optimization. Parsing. A query s trip through the DBMS. Validation. Logical plan

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University

Oracle Database: SQL and PL/SQL Fundamentals NEW

SQL functions fit into two broad categories: Data definition language Data manipulation language

Relational Query Languages. Relational Algebra and SQL. What is an Algebra? Select Operator. The Role of Relational Algebra in a DBMS

CSC 261/461 Database Systems Lecture 13. Fall 2017

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Relational Model & Algebra. Announcements (Thu. Aug. 27) Relational data model. CPS 116 Introduction to Database Systems

The SELECT-FROM-WHERE Structure

CS3DB3/SE4DB3/SE6M03 TUTORIAL

Explain in words what this relational algebra expression returns:

Transcription:

SQL - Data Query language Eduardo J Ruiz October 20, 2009 1 Basic Structure The simple structure for a SQL query is the following: select a1...an from t1... tr where C Where t 1... t r is a list of relations in the database schema, a 1... a n the elements in the resultant relation C a list of constraints. a i can be 1. A constant 2. An attribute in the resultant relation 3. A function applied to one attribute or several attributes 4. An aggregation function 5. * if we want to list all the attributes of a particular relation n is the arity of the resultant relation. C contains the list of restrictions that enforces the joins the selections Some queries are equivalent to π a1...a n σ C (t 1 t 2... t r ) Select all the attributes from the students with a GPA greater than 3.0 σ gpa>3.0 student select * where gpa > 3.0 The following query use a function to present the student full-name (concat is a mysql function) π pantherid,name+lastname (σ gpa>3.0 gender= F (student)) 1

select panterid, concat(name,,lastname) where gpa > 3.0 gender= F Cartesian product. All the students with all the courses. σ pantherid,courseid (student enrolled) select panterid, courseid,enrolled Query construction: 1. Select all the useful tables from the schema 2. Add the necessary join conditions (this is the most important step) 3. Add the necessary filter conditions 4. Select the attributes (apply fancy functions as needed) 5. Sort the results 2 Renaming You can rename tables attributes Why: to avoid ambiguity (two tables with the same attributes, self joins), to use clearer names, to add names to anonymous columns In the following example we rename the concatenated attribute to full name we add an extra attribute. Notice the complexity of the almost equivalent relational algebra expression) ρ(fellows(1 id, 2 fullname))(π pantherid,name+lastname (σ gpa>3.0 gender= F student)) select s.panterid as id, concat(s.name,,s.lastname) as fullname, fellow as description s where gpa > 3.95 In the following example we rename the tables to do a self join π s2 name σ s gpa>s2 gpa s name= Jhon ρ(asp (1 s name, 4 s gpa, 5 s2 name, 8 s2 gpa) (ρ(s, student) ρ(s2, student)) select s2.name s, student s2 where s.gpa > s2.gpa s.name= Jhon 2

3 Distinct Finding duplicates hurts the performance. For this reason the SQL query language does not remove duplicates Sometimes we want to remove duplicates from our output. You can force this with the DISTINTC clause Find all the courses that have failed at least one student in fall09. π courseid (σ grade= F term= F ALL09 (Student pantherid enrolled)) select distinct courseid from enrolled e where e.grade = F e.term= FALL09 Hint: Apply the distinct clause after you finish with the main body Hint: Do not use distinct when you don t need it (the optimizer is not as smart as you think) 4 Order by When: sorting reports Hint: in most case you don t want to sort in your application (the DBMS optimize sorts for disk storage) Order By: sort the result using a list of attributes a 1... a k Sort by a 1,breaks ties with a 2, breaks ties with a 3... breaks ties with a n. You must be careful with the default definition of order for each data type You can use the keywords ASC DESC to control if the results are sorted in ascending or descending order. In MySQL the default order is ascendant. select e.pantherid,s.name, e.grade, e.gpa s, enrolled e where (e.term= FALL09 OR e.term= SPRING09 ) e.courseid= COP5725 s.pantherid = e.pantherid order by grade, gpa desc Hint: Sort after you finish with the main body 5 UNION, INTERSECT, EXCEPT Read the book 3

6 Nested Queries In theory, a select statement returns a relation. Some DBMS allows to use this result in the FOR clause. We can also use correlated or uncorrelated queries. In this case the sub-queries appears in the WHERE clause as a condition The att IN sub-query condition returns true if the value of the attribute is in the resultant subquery relation. (You can think this as a look-up table). Notice that the arity of the sub-query should be the same as the number of atts The EXISTS sub-query returns true exists at least one tuple in the resultant sub-query relation. (Correlated Queries, PLUG a tuple TEST if the circuits is On/Off) The att > ANY sub-query returns true if exists at least one tuple t in the resultant sub-query relation, such that att > t The att > ALL sub-query returns true if for all the tuples t in the resultant sub-query relation, we have att > t Select all the students that are not recipient of the presidential fellowship have a gpa greater than 3.7 select pantherid where gpa > 3.7 pantherid not in (select sid from fellowship f where f.name = presidential ) select s.pantherid s where s.gpa > 3.7 not exists (select * from fellowship f where f.name = presidential f.sid = s.pantherid ) Select the student with the greatest GPA select s.pantherid s where not exists (select * s2 where s2.gpa > s.gpa) Select the student with the best grade in the database course (grades should be represented as numbers) select s.name s,enrolled e where s.pantherid = e.pantherid e.name = COP5725 4

e.term = FALL09 e.grade >= ALL(select e2.grade from enrolled e2 where e2.name = COP5725 e.term = FALL09 ) Both queries could have been expressed using as joins. Always write with joins if possible Efficiency depends on the size of the sub-query the number of tuples that are tested. Sometimes a IN can be faster, sometimes EXISTS could be faster 7 Aggregation Functions COUNT, SUM, AVG, MAX, MIN Aggregates the values of one column in the resultant relation You can not combine aggregation attributes with non grouped columns (you must use a group by) select avg(gpa),count(*),max(gpa),min(gpa) select name where gpa in (select max(gpa) ) 8 Group By The GROUP BY att clause split the table using the attributes in att All the tuples that have the same values in these GROUP BY attributes are part of the same group A simple implementation: sort by the same attributes find the groups You combine the GROUP BY clause with aggregation functions to collapse the group in one tuple (think as the attributes in the clause are keys of the group apply the function to the elements in this group) select courseid,term,count(*) from courses group by courseid, term select courseid,avg(gpa) s,enrolled e where e.pantherid = s.pantherid group by courseid 5

9 Having Sometimes you want to make a comparison of the values of the grouped tuples to filter the results Examples: 1. Select the courses such that the average grade is above some letter 2. Select all those students such that took at least 10 courses 3. Find those fellowships such that the average gpa of all the recipients is greater than 3.7 Hint: Find the groups. Apply the extra filter in the having. select f.name, avg(gpa) from fellowship f, student s where s.pantherid = f.sid group by f.name having avg(gpa) > 3.7 TYPICAL ERROR (THE AVG FUNCTION CAN NOT BE USED IN THE WHERE CLAUSE!!!) select f.name, from fellowship f, student s where s.pantherid = f.sid avg(gpa) > 3.7 10 Alternative Join Definition The SQL stard allows a syntax for defining joins that is more clear that the Cartesian semantics Joins are defined explicitly in the FROM clause Joins have different types: natural, inner, outher. The syntax also make easy to set hints to the optimizer select distinct pantherid, courseid s join enrolled e using (e.pantherid = s.pantherid) select distinct pantherid, courseid s natural join enrolled e 6