Structured Query Language (SQL) Ask and ye shall receive. The Bible.

Slides:



Advertisements
Similar presentations
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
Advertisements

4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Introduction to Structured Query Language (SQL)
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Database Management Systems & Programming Faculty of Information & Media Studies Summer 2000 LIS Week 6 Structured Query Language.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Structured Query Language Part I Chapter Three CIS 218.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Microsoft Access 2010 Chapter 7 Using SQL.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Introduction to SQL J.-S. Chou Assistant Professor.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language.
IMS 4212: Intro to SQL 1 Dr. Lawrence West, Management Dept., University of Central Florida Introduction to SQL—Topics Introduction to.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Chapter 9 Structured Query Language David M. Kroenke Database Processing © 2000 Prentice Hall.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
SQL “Structured Query Language; standard language for relational data manipulation” DB2, SQL/DS, Oracle, INGRES, SYBASE, SQL Server, dBase/Win, Paradox,
Intermediate SQL: Aggregated Data, Joins and Set Operators.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
1 Querying a Single Table Structured Query Language (SQL) - Part II.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
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.
Chapter 12 Subqueries and Merge Statements
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.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
SQL. Originally developed by IBM Standardized in 80’s by ANSI and ISO Language to access relational database and English-like non-procedural Predominant.
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.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
1 Introduction to Database Systems, CS420 SQL JOIN, Group-by and Sub-query Clauses.
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
References: Text Chapters 8 and 9
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Using Subqueries to Solve Queries
SQL Subquery.
Reporting Aggregated Data Using the Group Functions
Contents Preface I Introduction Lesson Objectives I-2
Database Systems: Design, Implementation, and Management Tenth Edition
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Aggregating Data Using Group Functions
Presentation transcript:

Structured Query Language (SQL) Ask and ye shall receive. The Bible

Class Outline  What is SQL?  What are the basic SQL select statements and what is the order in which they are to be used? Which are optional?  Create SQL statements that  use expressions and functions to convert data into information  use multiple criteria for searching  produce output in a specific order  provide summary data from groups of records  display data from two tables  show data that uses the results of another query as criteria

SQL  SQL is a non-procedural data access language that is used primarily in programming by embedding it in other languages  SQL is not a user-level language  SQL accepts one or more relations as input and produces a single relation as output  SQL provides functions for data definition (creates database and table structures), data management (enter, edit, delete data), and data query (convert data into information)  developed in mid 1970s by IBM; endorsed by ANSI (American National Standards Institute) as the language of choice for manipulating relational databases  language used by DB2, SQL/DS, ORACLE, INGRES, SYBASE, dBase, Paradox, Access (each with its own dialect)  computer systems are able to exchange data by passing SQL requests and responses to one another our focus

General SQL Query Syntax SELECT*columns to extract FROMtables containing columns WHEREsearch criteria to restricts rows that are returned GROUP BYsummarizes query results by groups HAVINGsearch criteria to restrict groups that are returned ORDER BYsorts results by one or more columns Preceding is the order in which clauses should appear Order of processing is as follows: From, Where, Group by, Having, Order by, Select required optional, must be in this order if any or all are used

Projections SELECT Name, Salary FROM Employee SELECT Office, EmpID, Name FROM Employee

Unique Projections SELECT DISTINCT Name, Salary FROM Employee SELECT DISTINCT Office, Salary FROM Employee appears only once

Selections SELECT EmpID, Name, Office, Salary FROM Employee WHERE Office = ‘Brussels’ SELECT * FROM Employee WHERE Office = ‘Brussels’

Combining Selections and Projections SELECT Name, Salary FROM Employee WHERE Office = ‘Brussels’ SELECT Name, Office, Salary FROM Employee WHERE EmpID = 35 Single quotes necessary around text and dates (but not values) in criteria.

Comparison Search Conditions SELECT Name, Salary FROM Employee WHERE Salary >= 2500 SELECT EmpID, Name, Office FROM Employee WHERE Name <> ‘Anne Abel’

Comparison Search Conditions

Examples of Search Conditions SELECT Name, Salary FROM Employee WHERE Office IN (‘Brussels’, ‘Tokyo’) SELECT Name, Office, Salary FROM Employee WHERE Salary between 2000 and 3000 SELECT Name, Office, Salary FROM Employee WHERE Name Like ‘%am’ SELECT Name, Office, Salary FROM Employee WHERE Name Like ‘Ab_l’ SELECT Name, Office, Salary FROM Employee WHERE Office is Null SELECT Name, Office, Salary FROM Employee WHERE Office NOT IN (‘Toronto’)

Compound Comparison Search Conditions SELECT Name, Salary FROM Employee WHERE (Office IN (‘Brussels’, ‘Tokyo’) or Salary is Null) and HireDate <= ‘7/15/99’ SELECT Name, Office, Salary FROM Employee WHERE (Salary between 2000 and 3000 and Office <> ‘Tokyo’) or (Name like ‘Gor% and EmpID > 20) SELECT Name, Office, Salary FROM Employee WHERE Name NOT Like ‘%Abel’ and Salary >= 3100 SELECT Name, Office, Salary FROM Employee WHERE Office IN (‘Toronto’) or Name = ‘Anne Abel’ when operators are combined, brackets are evaluated first (inner to outer) AND means that ALL conditions must be met OR means that ANY condition may be met

Sorting SELECT Name, Salary FROM Employee WHERE Office = ‘Tokyo’ ORDER BY Salary, Name SELECT Name, Office, Salary FROM Employee WHERE Salary >= 2000 ORDER BY EmpID DESC

Expressions using Arithmetic SELECT Name, Salary, Commission/Salary FROM Salesperson WHERE Commission >.05*Salary SELECT ContactName, CompanyName FROM Customer WHERE Paid is null and OrderDate >= ‘1/1/99’ ORDER BY SysDate-InvoiceDate SELECT ItemName, Price*1.15 FROM Product SELECT Name, (Sysdate-Birthdate)/365 FROM Employee ORDER BY Name SELECT EmpID, Hiredate+90 FROM Employee ORDER BY Name current system date

SQL Built-in Functions SELECT Count(*) FROM Employee SELECT Count(Distinct Office) FROM Employee SELECT Sum(Salary) FROM Employee SELECT Max(HireDate) FROM Employee SELECT Min(Name) FROM Employee SELECT Avg(Salary) FROM Employee

Aggregate Functions and Grouping SELECT Office, Count(*) FROM Employee GROUP BY Office SELECT Office, Status, Max(Salary) FROM Employee GROUP BY Office, Status To view the groupings, you must also select them!

More Grouping Functions SELECT Category, Avg(Price), Min(Quantity), Sum(Price*Quantity) FROM Product WHERE SupplierID in (1, 2) or SupplierID is null GROUP BY Category ORDER BY Avg(Price)

Restrict Groups with “Having” SELECT Category, Avg(Price), Min(Quantity), Sum(Price*Quantity) FROM Product WHERE SupplierID in (‘1’, ‘2’) or SupplierID is null GROUP BY Category HAVING Min(Quantity) < 5 The ‘WHERE’ clause is always evaluated before the ‘HAVING’ clause.

Another ‘Having’ example SELECT Office, Max(Salary) FROM Employee WHERE Status = ‘full-time’ GROUP BY Office HAVING Count(*) > 1 ORDER BY Office

Subqueries SELECT ProdID, Price, Qty FROM Product WHERE SupID IN (SELECT SupplierID FROM Supplier WHERE City = ‘London’) Subquery is always evaluated before the main query. SELECT ProdID FROM Product WHERE Qty > (SELECT Avg(Qty) FROM Product WHERE SupID IN (‘1’, ‘2’))

Nested Subqueries SELECT Count(Unique PolicyNum) FROM PolicyPlan WHERE PlanCode = ‘45’ and PolicyNum IN (SELECT PolicyNum FROM Commission WHERE AgentNum IN (SELECT AgentNum FROM Agent WHERE Area = 100)) Determine the number of policies sold in area 100 of type 45:

Join (Natural Join) SELECT Product ProdID, Product ProdDesc, Supplier SupplierName FROM Product, Supplier WHERE Product SupID = Supplier SupplierID Table names required in Select statement only if there’s a possibility of ambiguity

Renaming Attributes and Relations with an Alias SELECT SupplierName, avg(price) “Average Price”, count(*) “# of Items” FROM Product P, Supplier S WHERE P SupID = S SupplierID and Category = ‘Accessories’ GROUP BY SupplierName ORDER BY SupplierID alias

‘Exists’ and ‘not exists’ in Subqueries SELECT Name FROM Employee E WHERE EXISTS (SELECT * FROM Dependent D WHERE E.EmpID = D.EmpID and E.Sex = D.DepSex) Which employees have dependents of the same sex as themselves? Which employees have no dependents? SELECT Name FROM Employee E WHERE NOT EXISTS (SELECT * FROM Dependent D WHERE E.EmpID = D.EmpID)