A taxonomy of SQL queries Learning Plan

Similar documents
Database design process

COSC344 Database Theory and Applications. σ a= c (P) S. Lecture 4 Relational algebra. π A, P X Q. COSC344 Lecture 4 1

Basic SQL II. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

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

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

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

SQL STRUCTURED QUERY LANGUAGE

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

Overview Relational data model

Database Technology. Topic 3: SQL. Olaf Hartig.

Slides by: Ms. Shree Jaswal

Session Active Databases (2+3 of 3)

Announcement5 SQL5. Create%and%drop%table5. Basic%SFW%query5. Reading%a%table5. TDDD37%% Database%technology% SQL5

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

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

L130 - DATABASE MANAGEMENT SYSTEMS LAB CYCLE-1 1) Create a table STUDENT with appropriate data types and perform the following queries.

ECE 650 Systems Programming & Engineering. Spring 2018

SQL: Advanced Queries, Assertions, Triggers, and Views. Copyright 2012 Ramez Elmasri and Shamkant B. Navathe

Database Technology. Topic 2: Relational Databases and SQL. Olaf Hartig.

Advanced Databases. Winter Term 2012/13. Prof. Dr. Dietmar Seipel University of Würzburg. Advanced Databases Winter Term 2012/13

ECE 650 Systems Programming & Engineering. Spring 2018

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries

SQL. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

DBMS LAB SESSION PAVANKUMAR MP

Chapter 8. Joined Relations. Joined Relations. SQL-99: Schema Definition, Basic Constraints, and Queries

CS 348 Introduction to Database Management Assignment 2

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

Part 1 on Table Function

SQL-99: Schema Definition, Basic Constraints, and Queries. Create, drop, alter Features Added in SQL2 and SQL-99

Practical Project Report

CSC 742 Database Management Systems

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Advanced Databases (SE487) Prince Sultan University College of Computer and Information Sciences

Some different database system architectures. (a) Shared nothing architecture.

Data Definition Language (DDL)

Chapter 8: The Relational Algebra and The Relational Calculus

CIS611 Lab Assignment 1 SS Chung

Chapter 8: Relational Algebra

Simple SQL Queries (contd.)

Chapter 3. Algorithms for Query Processing and Optimization

Chapter 18 Strategies for Query Processing. We focus this discussion w.r.t RDBMS, however, they are applicable to OODBS.

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

Chapter 19 Query Optimization

The Relational Algebra and Calculus. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

CS5300 Database Systems

Ref1 for STUDENT RECORD DB: Ref2 for COMPANY DB:

CS 338 Basic SQL Part II

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CSIE30600 Database Systems Basic SQL 2. Outline

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

CIS 611. Copyright 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6-1

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe Slide 25-1

Relational Algebra 1

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

Hand in: the database picture (png or jpeg or ) for question 2, the queries (as SQL statements) for question 4

DATABASE CONCEPTS. Dr. Awad Khalil Computer Science & Engineering Department AUC

Algorithms for Query Processing and Optimization. 0. Introduction to Query Processing (1)

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Chapter 6 5/2/2008. Chapter Outline. Database State for COMPANY. The Relational Algebra and Calculus

SQL Introduction. CS 377: Database Systems

Government of Karnataka Department of Technical Education Bengaluru

Database Lab#9 Connecting to postgresql database from java programs

Integrity Coded Relational Databases (ICRDB) - Protecting Data Integrity in Clouds

MIT Database Management Systems Lesson 03: ER-to-Relational Mapping

Information Systems Development 37C Lecture: Final notes. 30 th March 2017 Dr. Riitta Hekkala

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

Querying a Relational Database COMPANY database For Lab4, you use the Company database that you built in Lab2 and used for Lab3

Chapter 6: RELATIONAL DATA MODEL AND RELATIONAL ALGEBRA

Relational Algebra. Relational Algebra Overview. Relational Algebra Overview. Unary Relational Operations 8/19/2014. Relational Algebra Overview

RELATIONAL DATA MODEL

Outline. Note 1. CSIE30600 Database Systems More SQL 2

COSC Assignment 2

Chapter 6 The Relational Algebra and Calculus

Relational Calculus: 1

NOTE: DO NOT REMOVE THIS EXAM PAPER FROM THE EXAM VENUE

The Mixed Ontology Building Methodology Using Database Information

Course Notes on Relational Algebra

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

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

This chapter discusses how to design a relational

Query 2: Pnumber Dnum Lname Address Bdate 10 4 Wallace 291 Berry, Bellaire, TX Wallace 291 Berry, Bellaire, TX

CSEN 501 CSEN501 - Databases I

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

Chapter 6 Part I The Relational Algebra and Calculus

Chapter 5 Relational Algebra. Nguyen Thi Ai Thao

Relational Model. CS 377: Database Systems

DEPARTMENT DNAME DNUMBER MGRNAME MGRSTARTDATE

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

Structured Query Language (SQL)

UNIVERSITY OF TORONTO MIDTERM TEST SUMMER 2017 CSC343H Introduction to Databases Instructor Tamanna Chhabra Duration 120 min No aids allowed

The Relational Algebra

Processor : Intel Pentium D3.0 GigaHtz

Relational Database Systems Part 01. Karine Reis Ferreira

UNIT 2 RELATIONAL MODEL

PES Institute of Technology Bangalore South Campus (1 K.M before Electronic City,Bangalore ) Department of MCA. Solution Set - Test-II

Transcription:

A taxonomy of SQL queries Learning Plan a. Simple queries: selection, projection, sorting on a simple table i. Small-large number of attributes ii. Distinct output values iii. Renaming attributes iv. Computed attributes v. Simple-complex conditions (AND, OR, NOT) vi. Partial Matching (LIKE % _ *?) vii. ASC-DESC ordering combinations viii. Checking for NULLs ix. DECODE, CASE, WHEN operators b. Multi-table queries i. Simple joins (no INNER JOIN) ii. Aliasing tables Full/Partial name qualification iii. Inner-joins (two and more (different) tables) iv. Inner-recursive-joins (joining to itself) v. Outer-joins (restrictions as part of the WHERE and ON clauses) c. Nested queries i. In, Not In ii. Exists, Not Exists iii. Dynamic relations (as part of SELECT, FROM, and WHERE clauses) d. Set Oriented Operations i. Union ii. Difference iii. Intersection iv. Division e. Summary queries i. On a simple table ii. On more than one table iii. Using nested/dynamic expressions iv. Using where & having clauses f. Maintenance Operations i. Simple insertion (insert into table values ( ) ) ii. Inserting rows from existing tables (create/append to table as select ) iii. Delete rows iv. Update rows v. Complex conditions given in the WHERE clause as select stmt. vi. Drop table command g. Administration i. Creating objects: tables, views, users, sequences, triggers, etc. ii. Privilege management through the Grant/Revoke commands iii. Transaction processing using Commit/Rollback iv. Backups, Legacy data, migration Guide-Learning-SQL-Examples. V. Matos. 1

Examples a. Simple Queries Small-large number of attributes select * from employee; select fname, lname from employee; Distinct output values select distinct salary from employee; Renaming attributes select distinct salary as "Monthly Payment" from employee; Computed attributes select salary as "USD", (salary*0.78) as "Euros" from employee Simple-complex conditions (AND, OR, NOT) select Fname, Lname, superssn from employee where dno = 4 and sex = 'F' and not (superssn= 123456789) Partial Matching (LIKE % _ *?) select Fname, Lname from employee where Lname like 'W%' select Fname, Lname, address from employee where address like '%TX%' and Fname like '_a%' ASC-DESC ordering combinations select fname, salary, dno from employee order by salary select fname, salary, dno from employee order by salary DESC select fname, salary, dno from employee order by dno ASC, salary DESC Checking for NULLs select fname, superssn from employee where (superssn is null) select fname, superssn from employee where NOT (superssn is null) DECODE, CASE, WHEN operators select Fname, Lname, DECODE(superssn, null, 'N.A.', superssn) from employee SELECT CASE sex WHEN 'F' THEN 'Ms. ' WHEN 'M' THEN 'Mr. ' ELSE ' ' END, fname, lname, salary FROM employee Guide-Learning-SQL-Examples. V. Matos. 2

b. Multi-table queries Simple joins (no INNER JOIN) select Fname, Lname, Dname from employee, department where dno = dnumber Aliasing tables Full/Partial name qualification select e.fname, e.lname, d.dname from employee e, department d where e.dno = d.dnumber Inner-joins (two and more (different) tables) select e.fname, e.lname, d.dname from employee e INNER JOIN department d ON (e.dno = d.dnumber) where e.sex = 'F'; Inner-recursive-joins (joining to itself) select e.fname as "Employee", b.fname as "Boss" from employee e, employee b where e.superssn = b.ssn Outer-joins (restrictions as part of the WHERE and ON clauses) ***INCORRECT VERSION*** select e.fname, e.lname, d.dependent_name as "Spouse" from employee e LEFT JOIN dependent d ON (e.ssn = d.essn) where d.relationship = 'Spouse' ***CORRECT VERSION*** select e.fname, e.lname, d.dependent_name as "Spouse" from employee e LEFT JOIN dependent d ON (e.ssn = d.essn) AND d.relationship = 'Spouse' Guide-Learning-SQL-Examples. V. Matos. 3

c. Nested queries In, Not In select fname from employee where dno IN (1,4,5); select Fname, Lname from employee where ssn IN (select essn from dependent where relationship='spouse') select Fname, Lname from employee where ssn NOT IN (select essn from dependent where relationship='spouse') Exists, Not Exists select e.fname, e.lname from employee e where exists (select * from dependent d where d.relationship='spouse' and e.ssn = d.essn) Dynamic relations (as part of SELECT, FROM, and WHERE clauses) select e.fname as "The Employee", d.dependent_name as "Spouse" from employee e, (select * from dependent where relationship = 'Spouse') d where e.ssn = d.essn d. Set Oriented Operations Union select Fname, dno from employee where sex = 'M' UNION select Fname, dno from employee where sex = 'F' Difference select ssn from employee MINUS select essn from dependent where relationship = 'Spouse' Intersection select essn from dependent where relationship = 'Spouse' INTERSECT select mgrssn from department Division select e.ssn from employee e where not exists (select * from project p where not exists (select * from works_on w where w.essn = e.ssn and w.pno=p.pnumber)) Guide-Learning-SQL-Examples. V. Matos. 4

e. Summary queries On a simple table select dno, avg(salary) as "AVG SALARY" from employee group by dno On more than one table select d.dname, avg(e.salary) as "AVG SALARY" from employee e, department d group by d.dname Using nested/dynamic expressions Using where & having clauses select dno, avg(salary) as "AVG SALARY" from employee group by dno having count(*) >= 2 f. Maintenance Operations Simple insertion (insert into table values ( ) ) Insert into WORKS_ON values (123456789, P77, 40) Insert into PROJECT values ( P77, Automation, null, Cleveland ) Inserting rows from existing tables (create/append to table as select ) MS-ACCESS Select * INTO Ladies from employee where sex = F ORACLE Create table Ladies as select * from employee where sex = F Delete rows Delete employee where ssn = 123456789 Delete works_on where pno = P77 Update rows Update employee set superssn=55555555, dno=4 where ssn=123456789 Update employee set salary = 1000000 where ssn = 123456789 Update employee set salary = salary + 1000 where dno =7 Complex conditions given in the WHERE clause as select stmt. Update employee set salary=salary*1.15 where ssn IN (select essn from works_on group by essn where count(pno) > 7) Guide-Learning-SQL-Examples. V. Matos. 5

Drop table command Drop table employee compare with Delete from employee g. Administration Creating objects: tables, views, users, sequences, triggers, etc. Create table customer (custname Text, custbalance Number) Create user Maria identified as Macarena Create Index, Cluster, Tablespace, Sequence, Segment, Profile, Trigger, Privilege management through the Grant/Revoke commands Grant select, insert, update, delete on employee to maria Revoke update on Employee from maria Transaction processing using Commit/Rollback Commit Rollback Guide-Learning-SQL-Examples. V. Matos. 6

Company Database PROJECT PNAME PNUMBER PLOCATION DNUM WORKS_ON ESSN PNO Hours DEPARTMENT DNAME DNUMBER MGRSSN MGRSTARTDATE EMPLOYEE FNAME MINIT LNAME SSN BDATE ADDRESS SEX SALARY SUPERSSN DNO DEPENDENT ESSN DEPENDENT_NAME SEX BDATE RELATIONSHIP DEPT_LOCATIONS DNUMBER DLOCATION Guide-Learning-SQL-Examples. V. Matos. 7

Reference: Elmasri / Navathe. "Fundamentals of Database Systems". Ed. Addison/Wesley Pub Co. 7th Edition, (2016). ISBN-13: 978-0133970777 Guide-Learning-SQL-Examples. V. Matos. 8