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

Similar documents
EECS 647: Introduction to Database Systems

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

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

CMPT 354: Database System I. Lecture 2. Relational Model

Chapter 6 The database Language SQL as a tutorial

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

The Relational Model. Suan Lee

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Querying Data with Transact SQL

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

SQL Overview. CSCE 315, Fall 2017 Project 1, Part 3. Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch

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

Chapter 3: Introduction to SQL

CS 582 Database Management Systems II

CSEN 501 CSEN501 - Databases I

Simple queries Set operations Aggregate operators Null values Joins Query Optimization. John Edgar 2

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

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

Chapter 4 SQL. Database Systems p. 121/567

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

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

QQ Group

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

RELATIONAL ALGEBRA II. CS121: Relational Databases Fall 2017 Lecture 3

Database Systems SQL SL03

Chapter 3: Introduction to SQL

Database Management Systems. Chapter 5

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

INTERMEDIATE SQL GOING BEYOND THE SELECT. Created by Brian Duffey

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

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

CS143: Relational Model

Relational Algebra and SQL

Database Systems SQL SL03

SQL - Data Query language

Simple SQL Queries (2)

COMP 244 DATABASE CONCEPTS & APPLICATIONS

Oracle Database 10g: Introduction to SQL

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

CS425 Midterm Exam Summer C 2012

NULL. The special value NULL could mean: Unknown Unavailable Not Applicable

COMP 244 DATABASE CONCEPTS AND APPLICATIONS

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

Querying Data with Transact-SQL

Advanced SQL GROUP BY Clause and Aggregate Functions Pg 1

Introduction to Databases, Fall 2005 IT University of Copenhagen. Lecture 2: Relations and SQL. September 5, Lecturer: Rasmus Pagh

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

Querying Data with Transact-SQL

Relational Query Languages

Database Management Systems. Chapter 5

Structured Query Language Continued. Rose-Hulman Institute of Technology Curt Clifton

Unit Assessment Guide

COMP 430 Intro. to Database Systems

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

Microsoft Querying Data with Transact-SQL - Performance Course

Multisets and Duplicates. SQL: Duplicate Semantics and NULL Values. How does this impact Queries?

Oracle 1Z Oracle Database 11g SQL Fundamentals I. Download Full Version :

SQL: Structured Query Language Nested Queries

Relational Database Languages

The SELECT-FROM-WHERE Structure

The Relational Model of Data (ii)

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

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

Oracle Database 11g: SQL and PL/SQL Fundamentals

Learn Well Technocraft

CPS221 Lecture: Relational Database Querying and Updating

T-SQL Training: T-SQL for SQL Server for Developers

Chapter 4: SQL. Basic Structure

CPS221 Lecture: Relational Database Querying and Updating

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries

G64DBS Database Systems. Lecture 7 SQL SELECT. The Data Dictionary. Data Dictionaries. Different Sections of SQL (DDL) Different Sections of SQL (DCL)

Part I: Structured Data

The SELECT-FROM-WHERE Structure

Relational Algebra for sets Introduction to relational algebra for bags

An Introduction to Structured Query Language

Midterm Review. Winter Lecture 13

STIDistrict Query (Basic)

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

1) Introduction to SQL

SQL: Data Manipulation Language

Overview of db design Requirement analysis Data to be stored Applications to be built Operations (most frequent) subject to performance requirement

Oracle Database: SQL and PL/SQL Fundamentals NEW

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

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

Review. The Relational Model. Glossary. Review. Data Models. Why Study the Relational Model? Why use a DBMS? OS provides RAM and disk

King Fahd University of Petroleum and Minerals

Lecture 2 SQL. Announcements. Recap: Lecture 1. Today s topic. Semi-structured Data and XML. XML: an overview 8/30/17. Instructor: Sudeepa Roy

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

An Introduction to Structured Query Language

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

MTA Database Administrator Fundamentals Course

The Relational Model

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

The Relational Model. Outline. Why Study the Relational Model? Faloutsos SCS object-relational model

An Introduction to Structured Query Language

Explain in words what this relational algebra expression returns:

Relational terminology. Databases - Sets & Relations. Sets. Membership

Transcription:

SQL 1

Database Languages A DBMS provides two types of languages: DDL Data Definition Language Language for defining a database schema DML Data Manipulation Language Language for accessing & manipulating the data 2

SQL (Structured Query Language) DDL DML 3

Simple Queries in SQL SELECT L A list of expressions indicates which attributes should appear in the output. FROM R A relation gives the relation(s) the query refers to WHERE C A condition is a Boolean expression indicating which tuples are of interest 4

Order of Clauses in SQL Query SELECT FROM WHERE GROUP BY Having ORDER BY 5

Exercise #1 Find all the courses information in database Course coursenumber name credit 346 Operating Systems 4 353 Databases 4 6

Exercise #1 Find all the courses information in database SELECT * FROM Course Star as List of All Attributes Query Result: Course coursenumber name credit 346 Operating Systems 4 353 Databases 4 No WHERE clause OK. The query result is unnamed 7

Exercise #2 Find all the students ids enrolled in at least one course EnrolledIn ID coursenumber grade 100 346 A+ 200 353 B 100 353 A- 200 346 B 8

Exercise #2 Find all the students ids enrolled in at least one course SELECT ID FROM EnrolledIn EnrolledIn ID coursenumber grade 100 346 A+ 200 353 B 100 353 A- 200 346 B a) ID 100 200 100 200 b) ID 200 100 The query result is a bag. 9

Exercise #3 Find all the students ids enrolled in 353 EnrolledIn ID coursenumber grade 100 346 A+ 200 353 B 100 353 A- 200 346 B 10

Exercise #3 Find all the students ids enrolled in 353 EnrolledIn ID coursenumber grade 100 346 A+ 200 353 B 100 353 A- SELECT ID 200 346 B FROM EnrolledIn WHERE coursenumber =353 ID 200 100 11

Exercise #3 Find all the students ids enrolled in 353 SELECT ID AS Student_ID FROM EnrolledIn Renaming Attributes WHERE coursenumber =353 Student_ID 200 100 12

Exercise #4 Find all the students first and last names enrolled in at least one course Students(ID, firstname, lastname, GPA, address) EnrolledIn(ID, coursenumber, grade) 13

Exercise #4 Describe the tuples that would appear in the result: EnrolledIn ID coursenumber grade SELECT * FROM EnrolledIn, Students 100 346 A+ 200 353 B 100 353 A- 200 346 B Students ID firstname lastname GPA address 100 Joe Smith 3.4 45 Main St. 200 Sue Brown 4 32 Bay St. 300 Ann John 3.7 26 Pine St. 14

Exercise #4 Result=The Cartesian Product of two relations 15

Exercise #4 Find all the students first and last names enrolled in at least one course Students(ID, firstname, lastname, GPA, address) EnrolledIn(ID, coursenumber, grade) SELECT firstname, lastname FROM EnrolledIn, Students WHERE? 16

Exercise #4 17

Exercise #4 Find all the students first and last names enrolled in at least one course Students(ID, firstname, lastname, GPA, address) EnrolledIn(ID, coursenumber, grade) SELECT firstname, lastname FROM EnrolledIn E, Students S WHERE S.ID=E.ID Renaming Relations 18

Exercise #4 Find all the students first and last names enrolled in at least one course Students(ID, firstname, lastname, GPA, address) EnrolledIn(ID, coursenumber, grade) SELECT DISTINCT firstname, lastname FROM EnrolledIn E, Students S WHERE S.ID=E.ID Eliminating Duplicates 19

Exercise #5 Find all the students first and last names enrolled in COMP 353 Students(ID, firstname, lastname, GPA, address) EnrolledIn(ID, coursenumber, grade) 20

Exercise #6 Find all the students ID s enrolled in at least two courses. EnrolledIn ID coursenumber grade 100 346 A+ 200 353 B 200 346 B 300 353 A- 300 346 A+ 300 355 B 21

Grouping SELECT ID FROM EnrolledIn GROUP BY ID SELECT DISTINCT ID FROM EnrolledIn ID 100 200 300 22

Grouping SELECT ID FROM EnrolledIn GROUP BY ID SELECT DISTINCT ID FROM EnrolledIn ID 100 200 300 SELECT ID, count(coursenumber) FROM EnrolledIn GROUP BY ID ID count 100 1 200 2 300 3 23

Grouping SELECT ID FROM EnrolledIn GROUP BY ID SELECT DISTINCT ID FROM EnrolledIn ID 100 200 300 SELECT ID, count(coursenumber) FROM EnrolledIn GROUP BY ID ID count 100 1 200 2 300 3 24

Grouping SELECT ID FROM EnrolledIn GROUP BY ID SELECT ID, count(coursenumber) FROM EnrolledIn GROUP BY ID SELECT ID, coursenumber FROM EnrolledIn GROUP BY ID SELECT DISTINCT ID FROM EnrolledIn ID 100 200 300 ID count 100 1 200 2 300 3? Only those attributes mentioned in the GROUP BY clause may appear unaggregated in SELECT clause. 25

Exercise #7 Find all the students ID s enrolled in at least two courses. (you cannot use GROUP BY ) 26

Exercise #8 Find all the students ID s with max GPA. Students ID firstname lastname GPA 100 Joe Smith 3.4 200 Sue Brown 4 300 Ann John 3.7 27

Exercise #8 Find all the students ID s with max GPA. SELECT ID FROM Students WHERE GPA = MAX(GPA) the aggregate max cannot be used in the WHERE clause. using a subquery Students ID firstname lastname GPA 100 Joe Smith 3.4 200 Sue Brown 4 300 Ann John 3.7 28

Where vs. Having WHERE selects input rows before groups and aggregates are computed. It controls which rows go into the aggregate computation. Thus, the WHERE clause must not contain aggregate functions. it makes no sense to try to use an aggregate to determine which rows will be inputs to the aggregates. HAVING selects group rows after groups and aggregates are computed. HAVING clause always contains aggregate functions. Strictly speaking, you are allowed to write a HAVING clause that doesn't use aggregates, but it's seldom useful. The same condition could be used more efficiently at the WHERE stage. 29

Exercise #9 Find all the students ID s with max GPA. (You cannot use MAX) Students ID firstname lastname GPA 100 Joe Smith 3.4 200 Sue Brown 4 300 Ann John 3.7 30

Pattern Matching in SQL % any sequence of zero or more characters address LIKE %Montreal% A sequence of characters of any length containing Montreal _ any single character address LIKE H There must be exactly three characters in the string, the first of which must be an H. To search for a % or a _ character (for example Montreal% ) LIKE Montrealx% ESCAPE x Example: 'abcd' LIKE 'abcd' 'abcd' LIKE 'a%' 'abcd' LIKE '_b ' 'abcd' LIKE 'c 'abcd' NOT LIKE 'c true true true false true SELECT firstname, lastname FROM Students WHERE Montreal LIKE %Montreal% 31

Exercise #10 Find all the students who live in Montreal. (i.e. have string Montreal as part of their address) Students ID firstname lastname GPA address 100 Joe Smith 3.4 45 Main St. Montreal 200 Sue Brown 4 32 Bay St. Montreal H3H 2M8 300 Ann John 3.7 26 Pine St. Toronto 32

Truth-values True Unknown False Truth-values 1 1/2 0 a AND b Min(a,b) a OR b Max(a,b) NOT a 1-a WHERE condition Tuples with either unknown or false as value are excluded from the answer. 33

NULL Values NULL is not a constant : NULL+7 not a legal SQL expression How to ask if x has the value NULL? x IS NULL x IS NOT NULL What is the answer? x x? x 0? Any arithmetic operations (+,-,*,/) involving null values must return NULL Except for the COUNT(*) function, all aggregate functions perform a Nullelimination step, so that Null values are not included in the final result of the calculation Any comparison (<,>,=,<=,>=) involving a NULL value evaluate to UNKNOWN 34

Now Go Back to Exercise #9 Find all the students ID s with max GPA. (You cannot use MAX) Students ID firstname lastname GPA 100 Joe Smith 3.4 200 Sue Brown 4 300 Ann John 3.7 400 Joe Brown NULL What if we don t know one of the students GPA? (NULL value) 35

Exercise #11 What are AVG(M) and AVG(N)? Are they equal? R M N 10 10 50 50 60 60 NULL 0 36

Exercise #11 What are AVG(M) and AVG(N)? Are they equal? AVG( M ) (10 50 60) / 3 40 AVG( N) (10 50 60 0) / 4 30 R M N 10 10 50 50 60 60 NULL 0 The elimination of Null values is not equivalent to replacing those values with zero. In SQL, AVG(X) SUM(X)/COUNT(*) 37

Exercise #12 What is the output? 1) SELECT * FROM R WHERE M=NULL 2) SELECT * FROM R WHERE M <> 50 3) SELECT * FROM R R N M 1 50 2 10 3 20 4 NULL 4) SELECT * FROM R WHERE ( M = 50 ) OR NOT ( M = 50 ); 38

Exercise #13 Find all students whose address or phone number is known, but not both. Students ID firstname lastname GPA address phone 100 Joe Smith 3.4 45 Main St. NULL 200 Sue Brown 4 NULL NULL 300 Ann John 3.7 NULL 6579065 400 Joe Brown 3 32 Bay St. 7863590 39

http://users.encs.concordia.ca/~m_oran/ 40