Structured Query Language Introduction. Basic Select SELECT lname, fname, phone FROM employees; Employees Table LNAMEFNAMEPHONE JonesMark555-1087 SmithSara555-2222.

Slides:



Advertisements
Similar presentations
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Advertisements

TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
 Database is SQL1.mdb ◦ import using MySQL Migration Toolkit 
Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
The University of Akron Dept of Business Technology Computer Information Systems The Relational Model: Query-By-Example (QBE) 2440: 180 Database Concepts.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
SQL SQL (Structured Query Language) is used to define, query, and modify relational databases Every relational database system understands SQL SQL is standard:
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Concepts of Database Management Sixth Edition
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
Concepts of Database Management, Fifth Edition
Chapter 3 Single-Table Queries
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
Introduction to Databases Chapter 7: Data Access and Manipulation.
Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform.
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
Oracle Database Administration Lecture 2 SQL language.
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Concepts of Database Management Seventh Edition
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
1 Multiple Table Queries. 2 Objectives  Retrieve data from more than one table by joining tables  Using IN and EXISTS to query multiple tables  Nested.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Structured Query Language
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
SQL advanced select using Oracle 1 Multiple Tables: Joins and Set Operations Subqueries: Nested Queries.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement.
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
IFS180 Intro. to Data Management Chapter 10 - Unions.
DQL Statements Lab - 3 COMP 353 Summer
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
6/22/2018.
UNIT -4 NORMALIZATION.
Working with Tables: Join, Functions and Grouping
Database Management Systems
Access: SQL Participation Project
Structured Query Language
SQL Subquery.
Presentation transcript:

Structured Query Language Introduction

Basic Select SELECT lname, fname, phone FROM employees; Employees Table LNAMEFNAMEPHONE JonesMark SmithSara BillingsDavid DanceIvanna JonesMary BarkerBob WoodsRobin JonesMary

Where Clause SELECT lname, fname, phone FROM employees WHERE dept = ‘ACTG’; Employees Table LNAMEFNAMEDEPTPHONE ITR FINC BillingsDavidACTG DanceIvannaACTG ITR BarkerBobACTG ITR FINC

Virtual Fields SELECT lname, fname, phone, salary/12 FROM employees WHERE dept = ‘ACTG’; Employees Table LNAMEFNAMEDEPTPHONE ITR FINC BillingsDavidACTG DanceIvannaACTG ITR BarkerBobACTG ITR FINC SALARY/

Naming Virtual Fields SELECT lname, fname, phone, salary/12 AS MonthlySal FROM employees WHERE dept = ‘ACTG’; Employees Table LNAMEFNAMEDEPTPHONE ITR FINC BillingsDavidACTG DanceIvannaACTG ITR BarkerBobACTG ITR FINC MonthlySal

Order By Clause SELECT lname, fname, phone, salary/12 as MonthlySal FROM employees WHERE dept = ‘ACTG’ ORDER BY MonthlySal Desc LNAMEFNAMEPHONE BillingsDavid DanceIvanna BarkerBob MonthlySal

Order By Clause (multiple keys) SELECT lname, fname, phone, dept, salary FROM employees ORDER BY Dept ASC, Salary DESC LNAMEFNAMEDEPTPHONESALARY JonesMarkITR SmithSaraFINC BillingsDavidACTG DanceIvannaACTG JonesMaryITR BarkerBobACTG WoodsRobinITR JonesMaryFINC primary sort key secondary sort key

“Like” EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR Sara David Ivanna 32JonesMaryITR Bob Robin 37JonesMaryFINC Employees Table SEX M F F SELECT * FROM employees WHERE fname LIKE ‘M%’;

Combining Conditions Retrieve all Columns for employees working in ITR that have a salary less than

Combining Conditions EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR FINC49000 ACTG42000 ACTG60000 ITR70000 ACTG44000 ITR90000 FINC56000 Employees Table SEX M SELECT * FROM employees WHERE dept = ‘ITR’ AND salary < 50000;

Combining Conditions: English is not very structured Retrieve all Columns for all employees that work in the finance and ITR departments.

Combining Conditions EMPIDLNAMEFNAMEDEPTPHONESALARY ITR FINC ACTG ITR ACTG ITR FINC Employees Table SEX SELECT * FROM employees WHERE dept = ‘ITR’ AND dept=‘FINC’;

Combining Conditions EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR SmithSaraFINC ACTG 32JonesMaryITR ACTG 36WoodsRobinITR JonesMaryFINC Employees Table SEX M F F M F SELECT * FROM employees WHERE dept = ‘ITR’ OR dept=‘FINC’;

Combining Conditions: Order of Operation Retrieve all Columns for all employees that work in either the finance or ITR departments and that also make less than $50000.

Combining Conditions EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR SmithSaraFINC ACTG42000 ACTG JonesMaryITR ACTG WoodsRobinITR Employees Table SEX M F F M SELECT * FROM employees WHERE dept = ‘ITR’ OR dept=‘FINC’ and salary < 50000

Combining Conditions EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR SmithSaraFINC ACTG42000 ACTG60000 ITR70000 ACTG44000 ITR90000 FINC56000 Employees Table SEX M F SELECT * FROM employees WHERE (dept = ‘ITR’ OR dept=‘FINC’) and salary < 50000

IN comparator EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR SmithSaraFINC ACTG 32JonesMaryITR ACTG 36WoodsRobinITR JonesMaryFINC Employees Table SEX M F F M F SELECT * FROM employees WHERE dept IN(‘ITR’,’FINC’);

Distinct Key Word EMPIDLNAMEFNAMEDEPTPHONESALARY ITR FINC ACTG ITR ACTG ITR FINC Employees Table SEX SELECT DISTINCT dept FROM employees;

Analytic Functions EMPIDLNAMEFNAMEDEPTPHONESALARY Employees Table SEX SELECT AVG(salary) FROM employees; AVG(SALARY)

SQL Analytic Functions AVG( ) MIN( ) MAX( ) SUM( ) COUNT( ) VAR() STD()

Group Functions EMPIDLNAMEFNAMEDEPTPHONESALARY ITR45000 FINC49000 ACTG42000 ACTG60000 ITR70000 ACTG44000 ITR90000 FINC56000 Employees Table SEX SELECT dept, AVG(salary) FROM employees GROUP BY dept; AVG(SALARY)

Grouping by Multiple Attributes DEPT FINC ACTG ITR SEX F M F F M SELECT dept, sex, AVG(salary) FROM employees GROUP BY dept, sex; AVG(SALARY) FINCM No male finance employees

Having Clause Base Table Inter. Result Set 1 WHERE Inter. Result Set 2 GROUP BY Inter. Result Set 3 HAVING Final Results ORDER BY

Having Clause (example) EMPIDLNAMEFNAMEDEPTPHONESALARY ITR45000 FINC49000 ACTG42000 ACTG60000 ITR70000 ACTG44000 ITR90000 FINC56000 Employees Table SEX SELECT dept, AVG(salary) FROM employees GROUP BY dept HAVING AVG(salary)<50000; AVG(SALARY)

Nested Queries Group functions are likely candidates for Nested Queries. For Example: SELECT lname, fname, phone, dept, salary FROM employees WHERE salary = (Select max(salary) from employees); The above query provides information on the employee with the highest salary. The inner query (shown in blue) will execute first and provide a numeric value which can be used to evaluate the condition on the outer query.

Nested Queries (Example 2) A nested query can also be created by replacing the table name in a WHERE clause with a subquery. For example: SELECT count(dept) FROM (SELECT dept, avg(salary) FROM employees GROUP BY dept HAVING avg(salary) > 40000) The above query provides the number of departments that have an average salary greater than $40,000. The inner query produces a table. The outer query counts the rows In that table.

Retrieving Info from Multiple Tables Programs Table EMPIDLANGUAGE PROFICIENCY COBOLGOOD SQL JAVA GOOD EXCELLENT EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR SmithSaraFINC BillingsDavidACTG DanceIvannaACTG JonesMaryITR BarkerBobACTG WoodsRobinITR JonesMaryFINC Employees Table SEX M F M F F M M F VBGOOD VB SQL COBOL JAVA COBOLEXCELLENT GOOD EXCELLENT NAME COBOL SQL JAVA VB FULLNAME COmmon Business Oriented Language Structured Query Language JAVA Visual Basic Languages Table 37SQLFAIR

Key Relational Operators Join (Product) –Inner Join and Natural Inner Join –Outer Join and Natural Outer Join –Cross Join Intersect Minus (Difference) Union Divide (not implemented by most DBMS)

Primary Key Foreign Key Pairs: The Basis for Joins Programs Table EMPIDLANGUAGE PROFICIENCY COBOLGOOD SQL JAVA GOOD EXCELLENT EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR SmithSaraFINC BillingsDavidACTG DanceIvannaACTG JonesMaryITR BarkerBobACTG WoodsRobinITR JonesMaryFINC Employees Table SEX M F M F F M M F VBGOOD VB SQL JAVA COBOLEXCELLENT NAME COBOL SQL JAVA VB FULLNAME COmmon Business Oriented Language Structured Query Language JAVA Visual Basic Languages Table 37 COBOL GOOD 37SQLFAIR

Query Illustrating Join Programs Table EMPIDLANGUAGE PROFICIENCY COBOLGOOD SQL JAVA GOOD EXCELLENT Employees Table VBGOOD VB SQL JAVA COBOLEXCELLENT SELECT language, employees.empid, lname, fname, phone FROM programs INNER JOIN employees ON programs.empID= employees.empID EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR SmithSaraFINC BillingsDavidACTG DanceIvannaACTG JonesMaryITR BarkerBobACTG WoodsRobinITR JonesMaryFINC SEX M F M F F M M F 37 COBOL GOOD 37SQLFAIR

Join Syntax Programs Table EMPIDLANGUAGE COBOL SQL JAVA EMPIDLNAMEFNAMEPHONE 23JonesMark DanceIvanna JonesMary WoodsRobin JonesMary Employees Table VB SQL COBOL JAVA COBOL SELECT employees.empid, language, lname, fname, phone FROM programs INNER JOIN employees ON programs.empID= employees.empID 37SQL

Initial result set before projection and selection Programs Table Inner Join Employees Table EMPIDLANGUAGE PROFICIENCY COBOLGOOD SQL JAVA GOOD EXCELLENT VBGOOD VB SQL COBOL JAVA COBOLEXCELLENT GOOD EXCELLENT SELECT * FROM programs INNER JOIN employees ON programs.empID= employees.empID EMPIDLNAMEFNAMEDEPTPHONESALARY 31DanceIvannaACTG JonesMaryITR JonesMaryFINC SEX F F F 23JonesMarkITR M 23JonesMarkITR M 32JonesMaryITR F 32JonesMaryITR F 32JonesMaryITR F 36WoodsRobinITR M 36WoodsRobinITR M 36WoodsRobinITR M 23JonesMarkITR M 37SQLFAIR37JonesMaryFINC F

Join Result Set LANGUAGE COBOL SQL JAVA LNAMEFNAMEPHONE JonesMark DanceIvanna JonesMary WoodsRobin JonesMary VB SQL COBOL JAVA COBOL SELECT language, employees.empid, lname, fname, phone FROM programs INNER JOIN employees ON programs.empID = employees.empID JonesMark JonesMark JonesMary JonesMary JonesMary EMPID WoodsRobin WoodsRobin JonesMary SQL37

Further Selection of Joined Data LANGUAGE SQL LNAMEFNAMEPHONE DanceIvanna JonesMary SQL SELECT language, employees.empid, lname, fname, phone FROM programs INNER JOIN employees ON programs.empID = employees.empID WHERE Language = ‘SQL’ JonesMark JonesMary EMPID WoodsRobin

OLD Inner Join Syntax LANGUAGE SQL LNAMEFNAMEPHONE DanceIvanna JonesMary SQL SELECT language, employees.empid, lname, fname, phone FROM programs, employees WHERE programs.empID= employees.empID AND Language = ‘SQL’ JonesMark JonesMary EMPID WoodsRobin SELECT language, employees.empid, lname, fname, phone FROM programs INNER JOIN employees ON programs.empID= employees.empID WHERE Language = ‘SQL’ OLD NEW

Three Table Inner Join Syntax SELECT programs.empid, name, fullname, phone FROM programs, employees, languages WHERE programs.empID= employees.empID AND languages.name = programs.language AND Proficiency = ‘Excellent’ SELECT programs.empid, name, fullname, phone FROM programs INNER JOIN employees ON programs.empID= employees.empID INNER JOIN languages ON languages.name = programs.language WHERE Proficiency = ‘Excellent’ OLD NEW

New Natural Join Syntax (if column names are the same) LANGUAGE SQL LNAMEFNAMEPHONE DanceIvanna JonesMary SQL SELECT language, empid, lname, fname, phone FROM programs, employees WHERE programs.empID= employees.empID AND Language = ‘SQL’ JonesMark JonesMary EMPID WoodsRobin SELECT language, empid, lname, fname, phone FROM programs NATURAL INNER JOIN employees WHERE Language = ‘SQL’ OLD NEW

Outer Join Result Set LANGUAGE COBOL SQL JAVA LNAME FNAMEPHONE JonesMark DanceIvanna JonesMary WoodsRobin JonesMary VB SQL COBOL JAVA COBOL SELECT language, employees.empid, lname, fname, phone from programs RIGHT OUTER JOIN employees ON programs.empID = employees.empID JonesMark JonesMark JonesMary JonesMary JonesMary EMPID WoodsRobin WoodsRobin JonesMary SQL37 25SmithSara BillingsDavid BarkerBob

Outer Join Syntax LNAMEFNAMECOUNT(Language) SmithSara0 JonesMary4 SELECT employees.empID, lname, fname, count(language) FROM programs, employees WHERE programs.empID(+) = employees.empID GROUP BY employees.empID, lname, fname JonesMark3 BillingsDavid0 DanceIvanna1 SELECT employees.empID, lname, fname, count(language) FROM programs RIGHT OUTER JOIN employees ON programs.empID = employees.empID GROUP BY employees.empID, lname, fname OLD NEW (Note: There is new FULL OUTER JOIN also which is a simultaneous LEFT and RIGHT outer JOIN.) JonesMary2 BarkerBob0 WoodsRobin3 EMPID

Cross Join Syntax SELECT lname, fname, languages.name FROM employees, languages; SELECT lname, fname, languages.name FROM employees CROSS JOIN languages; OLD NEW (e.g. JOIN every row in languages table with every row in employees table. 8x4=32 rows returned)

When not to join? If a client program needs data from two tables that does not necessarily mean that a join should occur. Unless the data needs to be viewed as a single table it may be appropriate to simple retrieve to result sets.

Which Employees know both JAVA and COBOL? What is the Query? Programs Table EMPIDLANGUAGE PROFICIENCY COBOLGOOD SQL JAVA GOOD EXCELLENT EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR SmithSaraFINC BillingsDavidACTG DanceIvannaACTG JonesMaryITR BarkerBobACTG WoodsRobinITR JonesMaryFINC Employees Table SEX M F M F F M M F VBGOOD VB SQL COBOL JAVA COBOLEXCELLENT GOOD EXCELLENT NAME COBOL SQL JAVA VB FULLNAME COmmon Business Oriented Language Structured Query Language JAVA Visual Basic Languages Table 37SQLFAIR

Intersect Consider: Set R1 { a, b, d, f, h} Set R2 {b, c, f, k, p} Which elements are common between the two sets?

Intersect Consider: Set R1 { a, b, d, f, h} Set R2 {b, c, f, k, p} Which elements are common between the two sets? a b c d f p k h

Intersect Used for Desired Query: Programmers that know JAVA and COBOL SELECT empid FROM programs WHERE language = 'JAVA' INTERSECT SELECT empid FROM programs WHERE language = 'COBOL'; JAVA programmers COBOL programmers

Minus Used for Desired Query: Programmers that know JAVA but not COBOL SELECT empid FROM programs WHERE language = 'JAVA' MINUS SELECT empid FROM programs WHERE language = 'COBOL'; JAVA programmers COBOL programmers

Minus Used for Desired Query: Programmers that know COBOL but not JAVA SELECT empid FROM programs WHERE language = 'COBOL'; MINUS SELECT empid FROM programs WHERE language = 'JAVA' JAVA programmers COBOL programmers

UNION can be used to combine elements (rows) from two sets SELECT lname, fname, phone FROM customers WHERE city = 'Radford'; UNION SELECT lname, fname, phone FROM employees WHERE city = 'Radford' Example: Construct a single list of customer and employee phone numbers for people that live in Radford. Fieldnames do not have to match but the first set of field Names will be used for the result set. The fields must be of the same data type.

Combining Relational Operations SELECT empid, fname, lname, count(language) FROM employees NATURAL LEFT OUTER JOIN programs GROUP BY empid, fname, lname Like any math operations, relational operations can be Combined to handle more complex situations. Example: Give the empid, First Name, Last Name, and number of Programming languages knows for all employees: In this case an OUTER JOIN has been combined with a GROUP BY operation.

Need for CASE statement PROFICIENCY EXCELLENT GOOD EXCELLENT EMPID FAIR SELECT empID, proficiency FROM programs WHERE Language = 'SQL' ORDER BY proficiency;

Use of CASE statement PROFICIENCY EXCELLENT GOOD EXCELLENT EMPID FAIR SELECT empID, proficiency FROM programs WHERE Language = 'SQL' ORDER BY (CASE proficiency WHEN 'EXCELLENT' THEN 1 WHEN 'GOOD' THEN 2 WHEN 'FAIR' THEN 3 END);

Use of old DECODE statement PROFICIENCY EXCELLENT GOOD EXCELLENT EMPID FAIR SELECT empID, proficiency FROM programs WHERE Language = 'SQL' ORDER BY DECODE(proficiency, 'EXCELLENT', 1, 'GOOD',2, 'FAIR',3 );