CS 582 Database Management Systems II

Similar documents
Chapter 3: Introduction to SQL

Chapter 3: Introduction to SQL

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

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

QQ Group

CS425 Fall 2017 Boris Glavic Chapter 4: Introduction to SQL

CS3DB3/SE4DB3/SE6M03 TUTORIAL

Lecture 6 - More SQL

Relational Algebra. Procedural language Six basic operators

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

Explain in words what this relational algebra expression returns:

Chapter 4: SQL. Basic Structure

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

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

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

CS3DB3/SE4DB3/SE6M03 TUTORIAL

Chapter 6: Formal Relational Query Languages

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

Chapter 3: SQL. Chapter 3: SQL

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

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

Unit 2: SQL AND PL/SQL

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

Database Systems SQL SL03

CS34800 Information Systems. Course Overview Prof. Walid Aref January 8, 2018

DATABASE TECHNOLOGY - 1MB025

Textbook: Chapter 6! CS425 Fall 2013 Boris Glavic! Chapter 3: Formal Relational Query. Relational Algebra! Select Operation Example! Select Operation!

DATABASE TECHNOLOGY - 1MB025

DATABASTEKNIK - 1DL116

Database Systems SQL SL03

Relational Model. Nisa ul Hafidhoh

CSCB20 Week 2. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

The Extended Algebra. Duplicate Elimination. Sorting. Example: Duplicate Elimination

DATABASE DESIGN I - 1DL300

SQL - Data Query language

Simple SQL Queries (2)

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

The SQL database language Parts of the SQL language

DATABASE TECHNOLOGY. Spring An introduction to database systems

Querying Data with Transact-SQL

CS3DB3/SE4DB3/SE6DB3 TUTORIAL

Principles of Data Management

Querying Data with Transact SQL

SQL Retrieving Data from Multiple Tables

SQL. Char (30) can store ram, ramji007 or 80- b

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

CS425 Midterm Exam Summer C 2012

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

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

CSC Web Programming. Introduction to SQL

ERRATA for Database System Concepts, 6 th Edition, 2010 Silberschatz, Korth, and Sudarshan Last updated: December 6, 2010

CS127 Homework #3. Due: October 11th, :59 P.M. Consider the following set of functional dependencies, F, for the schema R(A, B, C, D, E)

1) Introduction to SQL

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Chapter 2: Intro to Relational Model

STRUCTURED QUERY LANGUAGE (SQL)

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

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

Database System Concepts

An Introduction to Structured Query Language

An Introduction to Structured Query Language

Oracle Database 11g: SQL and PL/SQL Fundamentals

Chapter 2: Intro to Relational Model

SQL Data Query Language

Query Processing & Optimization

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

Relational Algebra. ICOM 5016 Database Systems. Roadmap. R.A. Operators. Selection. Example: Selection. Relational Algebra. Fundamental Property

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

CMP-3440 Database Systems

Querying Data with Transact-SQL

Chapter 2: Relational Model

Database Management Systems,

ASSIGNMENT NO 2. Objectives: To understand and demonstrate DDL statements on various SQL objects

Data Manipulation Language (DML)

12. MS Access Tables, Relationships, and Queries

SQL - Lecture 3 (Aggregation, etc.)

Relational Model, Relational Algebra, and SQL

An Introduction to Structured Query Language

Relational Algebra and SQL

Relational model and basic SQL

Chapter 6 Formal Relational Query Languages

Intermediate SQL ( )

Natural-Join Operation

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

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

An Introduction to Structured Query Language

Full file at

Midterm Review. Winter Lecture 13

Chapter 11: Query Optimization

Fundamentals of Database Systems

SQL QUERIES. CS121: Relational Databases Fall 2017 Lecture 5

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

SQL Functions (Single-Row, Aggregate)

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

Database Usage (and Construction)

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

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

Oracle Database: SQL and PL/SQL Fundamentals NEW

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

EECS 647: Introduction to Database Systems

Transcription:

Review of SQL Basics

SQL overview Several parts Data-definition language (DDL): insert, delete, modify schemas Data-manipulation language (DML): insert, delete, modify tuples Integrity View definition Transaction control Embedded SQL and Dynamic SQL Authorization

Database Schema classroom (building,room number, capacity) department (dept name, building, capacity) course (course id, title, dept name, credits) instructor (ID, name, dept name, salary) section (course id, sec id, semester, year, building, room number, time slot id) teaches (ID, course id, sec id, semester, year) student (ID, name, dept name, tot credit) takes (ID, course id, sec id, semester, year, grade) advisor(s ID, i ID) time slot (time slot id, day, start time, end time) prereq (course id, prereq id)

Basic Query Structure A typical SQL query has the form: select A 1, A 2,, A n from r 1, r 2,, r m where P A i represents an attribute r i represents a relation P is a predicate The result of an SQL query is a relation.

The Select Clause The select clause lists the attributes desired in the result of a query corresponds to the projection ( ) operation of the relational algebra Example: find the names of all instructors: select name from instructor Note: SQL names are case insensitive (i.e., you may use upper- or lower-case letters E.g., Name NAME name Some people use upper case wherever we use bold font.

The Select Clause distinct SQL allows duplicates in relations as well as in query results. To force the elimination of duplicates, insert the keyword distinct after select. Find the names of all departments with instructor, and remove duplicates select distinct dept name from instructor The keyword all specifies that duplicates not be removed. select all dept name from instructor

The Select Clause An asterisk in the select clause denotes all attributes select * from instructor The select clause can contain arithmetic expressions involving the operation, +,,, and /, and operating on constants or attributes of tuples. The query: select ID, name, dept name, salary/12 from instructor would return a relation that is the same as the instructor relation, except that the value of the attribute salary is divided by 12.

The Where Clause The where clause specifies conditions that the result must satisfy Corresponds to the selection (σ) predicate of relational algebra. Example: find all instructors in Comp. Sci. dept with salary >80000 select name from instructor where dept name = Comp. Sci. and salary >80000 Comparison results can be combined using the logical connectives and, or, and not. Comparisons can be applied to results of arithmetic expressions.

The from Clause The from clause lists the relations involved in the query Corresponds to the Cartesian product ( ) operation of the relational algebra. Example: find the Cartesian product instructor teaches select * from instructor, teaches generates every possible instructor teaches pair, with all attributes from both relations. Cartesian product is not very useful directly, but useful combined with where-clause condition.

Intro Basic Query Structure Set Operation Agg. Func. Subqueries Cartesian Product Cartesian Product teaches instructor Database System Concepts - 6th Edition 3.16CS Silberschatz, 582 Database Management Systems IIKorth and Sudarshan

Joins - Examples For all instructors who have taught courses, find their names and the course ID of the courses they taught. select name, course id from instructor, teaches where instructor.id = teaches.id Find the course ID, semester, year and title of each course offered by the Comp. Sci. department select section, course id, semester, year, title from section, course where section.course id = course.course id and dept name = Comp. Sci. More complicated joins: discussed later

Natural Join Natural Join Natural join matches tuples with the same values for all common attributes, and retains only one copy of each Natural join matches tuples with the same values for all common common column attributes, and retains only one copy of each common column select * from instructor natural join teaches; select * from instructor natural join teaches;

Natural Join Example Danger in natural join: beware of unrelated attributes with same name which get equated incorrectly Example: List the names of instructors along with the the titles of courses that they teach Incorrect version (equates course.dept name with instructor.dept name) select name, title from instructor natural join teaches natural join course; Correct version select name, title from instructor natural join teaches, course where teaches.course id= course.course id;

Ordering the Display of Tuples List in alphabetic order the names of all instructors select distinct name from instructor order by name We may specify desc for descending order or asc for ascending order, for each attribute; ascending order is the default. Example: order by name desc Can sort on multiple attributes Example: order by dept name desc, name asc

Duplicates In relations with duplicates, SQL can define how many copies of tuples appear in the result. Multiset versions of some of the relational algebra operators given multiset relations r 1 and r 2 : σ θ (r 1 ): If there are c 1 copies of tuple t 1 in r 1, and t 1 satisfies selections σ θ, then there are c 1 copies of t 1 in σ θ (r 1 ) A (r 1): For each copy of tuple t 1 in r, there is a copy of tuple A (t 1) in A (r 1), where A (t 1) denotes the projection of the single tuple t 1 r 1 r 2 : If there are c 1 copies of tuple t 1 in r 1 and c 2 copies of tuple t 2 in r 2, there are c 1 c 2 copies of the tuple t 1 t 2 in r 1 r 2

Duplicates (cont.) Example: Suppose multiset relations r 1 (A, B) and r 2 (C) are as follows: r 1 = {(1, a)(2, a)}, r 2 = {(2), (3), (3)} Then B (r 1) would be {(a), (a)}, while B (r 1) r 2 would be {(a, 2), (a, 2), (a, 3), (a, 3), (a, 3), (a, 3)} SQL duplicate semantics: select A 1, A 2,, A n from r 1, r 2,, r m where P is equivalent to the multiset version of the expression: A 1,A 2,,A n (σ P (r 1 r 2 r m ))

Set Operations Find courses that ran in Fall 2009 or in Spring 2010 (select course id from section where semester = Fall and year = 2009) union (select course id from section where semester = Spring and year = 2010) Find courses that ran in Fall 2009 and in Spring 2010 (select course id from section where semester = Fall and year = 2009) intersect (select course id from section where semester = Spring and year = 2010) Find courses that ran in Fall 2009 but not in Spring 2010 (select course id from section where semester = Fall and year = 2009) except (select course id from section where semester = Spring and year = 2010)

Set Operations (2) Set operations union, intersect, and except Each of the above operations automatically eliminates duplicates To retain all duplicates use the corresponding multiset versions union all, intersect all and except all. Suppose a tuple occurs m times in r and n times in s, then, it occurs: m + n times in r union all s min(m, n) times in r intersect all s max(0, m n) times in r except all s

Aggregate Functions These functions operate on the multiset of values of a column of a relation, and return a value avg: average value min: minimum value max: maximum value sum: sum of values count: number of values

Aggregate Functions Example Find the average salary of instructors in the Computer Science department select avg (salary) from instructor where dept name= Comp. Sci. ; Find the total number of instructors who teach a course in the Spring 2010 semester select count (distinct ID) from teaches where semester = Spring and year = 2010; Find the number of tuples in the course relation select count (*) from course;

Aggregate Functions - Group By Aggregate Functions Group By Find the average salary of instructors in each department select Find dept the average name, salary avg of (salary) instructors in each department from instructor select dept_name, avg (salary) from instructor group by dept name; group by dept_name; avg_salary Database System Concepts - 6 th Edition 3.33 Silberschatz, Korth and Sudarshan

Aggregation Functions Group By (2) Attributes in select clause outside of aggregate functions must appear in group by list /* erroneous query */ select dept name, ID, avg(salary) from instructor group by dept name;

Aggregate Functions - Having Clause Find the names and average salaries of all departments whose average salary is greater than 42000 select dept name, avg(salary) from instructor group by dept name having avg(salary)>42000; Note: predicates in the having clause are applied after the formation of groups whereas predicates in the where clause are applied before forming groups

Nested Subqueries SQL provides a mechanism for the nesting of subqueries. A subquery is a select-from-where expression that is nested within another query. A common use of subqueries is to perform tests for set membership, set comparisons, and set cardinality.