COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries



Similar documents
Chapter 5. SQL: Queries, Constraints, Triggers

Comp 5311 Database Management Systems. 3. Structured Query Language 1

Advance DBMS. Structured Query Language (SQL)

SQL: Queries, Programming, Triggers

SQL: Queries, Programming, Triggers

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves

Relational Algebra and SQL

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Oracle SQL. Course Summary. Duration. Objectives

Oracle Database: SQL and PL/SQL Fundamentals

UNIT 6. Structured Query Language (SQL) Text: Chapter 5

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6

Instant SQL Programming

Oracle Database: SQL and PL/SQL Fundamentals NEW

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

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Oracle Database: SQL and PL/SQL Fundamentals

Introduction to Microsoft Jet SQL

Oracle Database 12c: Introduction to SQL Ed 1.1

Oracle Database 10g: Introduction to SQL

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

Introduction to SQL ( )

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

IT2305 Database Systems I (Compulsory)

Introduction to SQL: Data Retrieving

Relational Database: Additional Operations on Relations; SQL

Chapter 4: SQL. Schema Used in Examples

IT2304: Database Systems 1 (DBS 1)

Structured Query Language (SQL)

Relational Algebra. Module 3, Lecture 1. Database Management Systems, R. Ramakrishnan 1

Information Systems SQL. Nikolaj Popov

Programming with SQL

Oracle Database 11g SQL

Oracle 10g PL/SQL Training

T-SQL STANDARD ELEMENTS

Database Query 1: SQL Basics

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

4. SQL. Contents. Example Database. CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain)

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

The Relational Model. Why Study the Relational Model?

Query-by-Example (QBE)

Microsoft Access 3: Understanding and Creating Queries

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Lecture 6. SQL, Logical DB Design

Part 4: Database Language - SQL

Introduction to SQL and SQL in R. LISA Short Courses Xinran Hu

Oracle Database: SQL and PL/SQL Fundamentals NEW

Relational Databases

MOC 20461C: Querying Microsoft SQL Server. Course Overview

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Oracle Database: Introduction to SQL

Oracle Database: Introduction to SQL

Summary on Chapter 4 Basic SQL

DBMS / Business Intelligence, SQL Server

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

Netezza SQL Class Outline

Introduction to SQL for Data Scientists

3. Relational Model and Relational Algebra

3.GETTING STARTED WITH ORACLE8i

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina


How To Create A Table In Sql (Ahem)

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

Oracle Database: Introduction to SQL

BCA. Database Management System

Introduction to SQL C H A P T E R3. Exercises

ICAB4136B Use structured query language to create database structures and manipulate data

Database Management System

Chapter 6: Integrity Constraints

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

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

SQL Programming. Student Workbook

A Brief Introduction to MySQL

Exercise 1: Relational Model

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables

Database Administration with MySQL

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

Chapter 1 Overview of the SQL Procedure

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7

Databases 2011 The Relational Model and SQL

Databases in Engineering / Lab-1 (MS-Access/SQL)

Financial Data Access with SQL, Excel & VBA


David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design and Implementation

Week 4 & 5: SQL. SQL as a Query Language

Handling Missing Values in the SQL Procedure

Access Queries (Office 2003)

Proposed solutions Project Assignment #1 (SQL)

Θεµελίωση Βάσεων εδοµένων

Discovering SQL. Wiley Publishing, Inc. A HANDS-ON GUIDE FOR BEGINNERS. Alex Kriegel WILEY

Introduction to database design

Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables. Lesson C Objectives. Joining Multiple Tables

Effective Use of SQL in SAS Programming

Introducción a las bases de datos SQL Libro de referencia

SQL. Short introduction

Relational Algebra. Query Languages Review. Operators. Select (σ), Project (π), Union ( ), Difference (-), Join: Natural (*) and Theta ( )

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

Transcription:

COMP 5138 Relational Database Management Systems Week 5 : Basic COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-1 Today s Agenda Overview Basic Queries Joins Queries Aggregate Functions and Set Operations Textbook, Chapter 5 (up to 5.6) COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-2 1

- The Structured Query Language is the standard declarative query language for RDBMS Describing what data we are interested in, but not how to retrieve it. Based on SEQUEL Introduced in the mid-1970 s as the query language for IBM s System (Structured English Query Language) ANSI standard since 1986, ISO-standard since 1987 1989: revised to -89 1992: more features added -92 1999: major rework :1999 ( 3) :2003 bugfix release of :1999 plus /XML COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-3 Overview DDL (Data Definition Language) Create, drop, or alter the relation schema DML (Data Manipulation Language) The retrieval of information stored in the database A Query is a statement requesting the retrieval of information The portion of a DML that involves information retrieval is called a query language The insertion of new information into the database The deletion of information from the database The modification of information stored in the database DCL (Data Control Language) Commands that control a database, including administering privileges and users COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-4 2

DDL Remember from last lecture / Week 3: Creation of tables (relations): CREATE TABLE name ( list_of_columns ) Create new relation with given name and list of columns Specify domain type for each column Also: Specify Integrity Constraints PRIMARY KEY and FOREIGN KEY REFERENCES parent_table NULL / NOT NULL constraints Much more on that in two weeks time Deletion of tables (relations): DROP TABLE name the schema information and the tuples are deleted. COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-5 DML Statements Insertion of new data into a table / relation Syntax: INSERT INTO table [ ( list-of-columns ) ] VALUES ( list-of-expression ) Example: INSERT INTO Students (sid, name) VALUES (53688, Smith ) Updating of tuples in a table / relation Syntax: UPDATE table SET column = expression {, column = expression} [ WHERE search_condition ] Example: UPDATE students SET gpa = gpa - 0.1 WHERE gpa >= 3.3 Deleting of tuples from a table / relation Syntax: DELETE FROM table [ WHERE search_condition ] Example: More details on those in a second... DELETE FROM Students WHERE name = Smith COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-6 3

Today s Agenda Overview Basic Queries Join Queries Aggregate Functions and Set Operations COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-7 SELECT Statement Used for queries on single or multiple tables Clauses of the SELECT statement: SELECT Lists the columns (and expressions) that should be returned from the query FROM Indicate the table(s) from which data will be obtained WHERE Indicate the conditions to include a tuple in the result GROUP BY Indicate the categorization of tuples HAVING Indicate the conditions to include a category ORDER BY Sorts the result according to specified criteria The result of an query is a relation A SFW-query is equivalent to the relational algebra expression Π A1, A2,..., An ( σ condition (R 1 x R 2 x... x R m ) ) COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-8 4

Running Example sid UoS_Code name birthdate Students enrolled Courses (unit_of_study) title country semester grade credit_ points mark assess teach Lecturer empid room name COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-9 Running Example - Database Schema sid enrolled UoS_code semester students sid name birthdate country courses UoS_code title credit_points lecturer assessment sid UoS empid mark lecturers empid name room COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-10 5

Example: Basic Query List the names of all Australian students. select name from students where country= AUS Corresponding relational algebra expression π name ( σ country= AUS (students) ) Note: does not permit the - character in names, and names are case insensitive, i.e. you can use capital or small letters. You may wish to use upper case where-ever we use bold font. COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-11 Example: Order By Clause List all students (name) from Australia in alphabetical order. select name from students where country= AUS order by name Two options (per attribute): ASC ascending oder (default) DESC descending order You can order by more than one attribute e.g., order by country desc, name asc COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-12 6

Duplicates In contrast to the relational algebra, allows duplicates in relations as well as in query results. To force the elimination of duplicates, insert the keyword distinct after select. Example: List the countries where students come from. select distinct country from students The keyword all specifies that duplicates not be removed. select all country from students COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-13 Arithmetic Expressions in Select Clause An asterisk in the select clause denotes all attributes SELECT * FROM students The select clause can obtain arithmetic expressions involving the operations +, -, * and /, and operating on constants or attributs of tuples. The query: SELECT uos_code, title, credit_points*2, lecturer FROM courses would return a relation which is the same as the courses relation except that the credit-point-values are doubled. COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-14 7

The Rename Operation allows renaming relations and attributes using the as clause: old_name as new_name This is very useful to give, e.g., result columns of expressions a meaningful name. Example: Find the name, loan number and loan amount of all customers; rename the column name loan_number as loan_id. select customer_name, borrower.loan_number as loan_id, amount from borrower, loan where borrower.loan_number = loan.loan_number COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-15 The WHERE Clause The where clause specifies conditions that the result must satisfy corresponds to the selection predicate of the relational algebra. Comparison operators in : =, >, >=, <, <=,!=, <> Comparison results can be combined using the logical connectives and, or, and not. Comparisons can be applied to results of arithmetic expr. Example: Find all UoS codes for courses taught by employee 1011 that are worth more than four credit points: SELECT uos_code FROM courses WHERE lecturer = 1011 AND credit_points > 4 COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-16 8

The WHERE Clause (cont d) includes a between comparison operator (called range queries ) Example: Find all students (by SID) who gained marks in the distinction and high-distinction range in COMP5138. SELECT sid FROM assessment WHERE uos_code = COMP5138 mark BETWEEN 75 AND 100 AND COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-17 String Operations includes a string-matching operator for comparisons on character strings. LIKE is used for string matching Patterns are described using two special characters ( wildcards ): percent (%). The % character matches any substring. underscore (_). The _ character matches any character. List the titles of all COMP unit of studies. select title from courses where uos_code like COMP% supports a variety of string operations such as concatenation (using ) converting from upper to lower case (and vice versa) finding string length, extracting substrings, etc. COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-18 9

The FROM Clause The from clause lists the relations involved in the query corresponds to the Cartesian product operation of the relational algebra. join-predicates must be explicitly stated in the where clause Examples: Find the Cartesian product students x courses SELECT * FROM student, courses Find the student ID, name, and gender of all students enrolled in COMP5138: SELECT sid, name, gender FROM students, enrolled WHERE students.sid = enrolled.sid AND uos_code = COMP5138 COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-19 Join Example Which students did enroll in what semester? Join involves multiple tables in FROM clause SELECT name, number, semester FROM Students S, Enrolled E WHERE S.sid = E.sid; WHERE clause performs the equality check for common columns of the two tables COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-20 10

Aliases Some queries need to refer to the same relation twice In this case, aliases are given to the relation name Example: For each employee, retrieve the employee's name, and the name of his or her immediate supervisor. SELECT FROM WHERE E.FNAME, E.LNAME, S.FNAME,S.LNAME EMPLOYEE E S E.SUPERSSN = S.SSN We can think of E and S as two different copies of EMPLOYEE; E represents employees in role of supervisees and S represents employees in role of supervisors COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-21 Today s Agenda Overview Basic Queries Join Queries Aggregate Functions and Set Operations COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-22 11

More on Joins Join a relational operation that causes two or more tables with a common domain to be combined into a single table or view Equi-join a join in which the joining condition is based on equality between values in the common columns; common columns appear redundantly in the result table Natural join an equi-join in which one of the duplicate columns is eliminated in the result table Outer join a join in which rows that do not have matching values in common columns are nonetheless included in the result table (as opposed to inner join, in which rows must have matching values in order to appear in the result table) Union join includes all columns from each table in the join, and an instance for each row of each table The common columns in joined tables are usually the primary key of the dominant table and the foreign key of the dependent table in 1:M relationships COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-23 Join Operators offers join operators to directly formulate the natural join, equi-join, and the theta join RA operations. R natural join S R inner join S on <join condition> R inner join S using (<list of attributes>) These additional operations are typically used as subquery expressions in the from clause List all students and in which courses they enrolled. select name, uos_code, semester from students natural join enrolled Who is teaching COMP5138? select name from courses inner join lecturers on lecturer=empid where uos_code= COMP5138 COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-24 12

More Join Operators Available join types: inner join left outer join right outer join full outer join Join Conditions: natural on <join condition> using <attribute list> e.g: students inner join enrolled using (sid) inner join result sid name birthdate country sid2 number grade 112 A 01.01.84 India 112 SOFT1 P 200 B 31.5.79 China 200 COMP2 C e.g : students left outer join enrolled using (sid) left outer join result sid name birthdate country sid2 number grade 112 200 210 A B C 01.01.84 India 31.5.79 China 29.02.82 Australia COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-25 112 200 null SOFT1 COMP2 null P C null Example Queries I List all courses by title select title from courses List all courses. select * from courses List in alphabetic order the details of all courses select * from courses order by title COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-26 13

Example Queries II Find the students with marks between 0 and 10. select sid from assessment where mark between 0 and 10 Who is teaching COMP5138? select name from courses, lecturers where number= COMP5138 and lecturer=empid ist students who got a (high) distinction in COMP5138. Tip: use in (not between) comparison operator for sets select sid from enrolled where number = COMP5138 and grade in ( D, H ) COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-27 Today s Agenda Overview Basic Queries Aggregate Functions and Set Operations Join Queries Grouping Nested Queries COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-28 14

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 Note: with aggregate functions you can t have single-valued columns included in the select clause COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-29 Examples for Aggregate Functions How many students enrolled? select count(*) from Enrolled select count(distinct sid) from Enrolled Which was the best mark for SOFT2007? select max(mark) from assessment where uos_code = SOFT2007 What was the average mark for SOFT2007? select avg(mark) from assessment where uos_code= SOFT2007 COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-30 15

Set Operations The set operations union, intersect, and except (Oracle: minus) operate on relations and correspond to the relational algebra operations,,. 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 (not supported by Oracle) (not supported by Oracle) COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-31 Set Operations Find all customers who have a loan, an account, or both: (select customer_name from depositor) union (select customer_name from borrower) Find all customers who have both a loan and an account (select customer_name from depositor) intersect (select customer_name from borrower) Find all customers who have an account but no loan (select customer_name from depositor) except (select customer_name from borrower) COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-32 16

Examples for Set Operations List all names in the database. select name from students union select name from lecturers Which students did not enroll in any course? select sid from students minus select sid from enrolled Find students who enrolled for COMP5138 and COMP5318. select sid from enrolled where number= COMP5138 intersect select sid from enrolled where number= COMP5318 COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-33 NULL Values It is possible for tuples to have a null value, denoted by null, for some of their attributes Integral part of to handle missing / unknown information null signifies that a value does not exist, it does not mean 0 or blank! The predicate is null can be used to check for null values e.g. Find students which enrolled in a course without a grade so far. SELECT sid FROM enrolled WHERE grade IS NULL Consequence: Three-valued logic The result of any arithmetic expression involving null is null e.g. 5 + null returns null However, (most) aggregate functions simply ignore nulls COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-34 17

NULL Values and Three Valued Logic Any comparison with null returns unknown e.g. 5 < null or null <> null or null = null Three-valued logic using the truth value unknown: OR: (unknown or true) = true, (unknown or false) = unknown (unknown or unknown) = unknown AND: (true and unknown) = unknown, (false and unknown) = false, (unknown and unknown) = unknown NOT: (not unknown) = unknown Result of where clause predicate is treated as false if it evaluates to unknown e.g: select sid from enrolled where grade = unknown ignores all students without a grade so far COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-35 NULL Values and Aggregation Aggregate functions except count(*) ignore null values on the aggregated attributes result is null if there is no non-null amount Examples: Average mark of all assignments SELECT AVG (mark) FROM assessment Number of all assignments SELECT COUNT (*) FROM assessment -- ignores nulls -- counts all tuples COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-36 18

Summary Relational Algebra Six basic operators Some additional rules / operators Overview DDL and DML Basic Query SELECT FROM WHERE ORDER BY Aggregate Functions and Set Operations Count, Sum, Min, Max, Avg, Union, Intersect, and Except COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-37 Next Week Advanced Nested Subqueries / Grouping Integrity Constraints Static Integrity Constraints Domain Constraints Referential Integrity Assertions Dynamic Integrity Constraints Triggers Security and Authorization Textbook Sections 3.2 and 3.3 Sections 5.7 to 5.9 Sections 21.1 to 21.3 COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-38 19

Benefits of a Standardized DB Language Reduced training costs Productivity Application portability Application longevity Reduced dependency on a single vendor Cross-system communication COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-39 Exercise Example 2 S1 sid sname rating age R1 sid bid day 22 101 10/10/96 58 103 11/12/96 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 B1 bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-40 20

Example Query 1 Find sid s of sailors who ve reserved a red or a green boat SELECT FROM WHERE SELECT FROM WHERE UNION SELECT FROM WHERE S.sid Sailors S, Boats B, Reserves R S.sid=R.sid AND R.bid=B.bid AND (B.color= red OR B.color= green ) S.sid Sailors S, Boats B, Reserves R S.sid=R.sid AND R.bid=B.bid AND B.color= red S.sid Sailors S, Boats B, Reserves R S.sid=R.sid AND R.bid=B.bid AND B.color= green COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-41 Example Query 2 Find sid s of sailors who ve reserved a red and a green boat SELECT S.sid FROM Sailors S, Boats B1, Reserves R1, Boats B2, Reserves R2 WHERE S.sid=R1.sid AND R1.bid=B1.bid AND S.sid=R2.sid AND R2.bid=B2.bid AND (B1.color= red AND B2.color= green ) SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color= red INTERSECT SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color= green COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-42 21