SQL.

Slides:



Advertisements
Similar presentations
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Advertisements

1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Aggregate Functions Presenter: Sushma Bandekar CS 157(A) – Fall 2006.
CS411 Database Systems Kazuhiro Minami 06: SQL. Join Expressions.
Data Warehousing/Mining 1 Data Warehousing/Mining Comp 150 Aggregation in SQL (not in book) Instructor: Dan Hebert.
Chapter 11 Group Functions
LECTURE 10.  Group functions operate on sets of rows to give one result per group.
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
--The SQL Query Language DML--1 The SQL Query Language DML The SQL Query Language DML (SELECT)
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
Querying a Database Using the Select Query Window
1 SQL-Structured Query Language SQL is the most common language used for creating and querying relational databases. Many users can access a database applications.
Murali Mani SQL. Murali Mani SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”  (sName=“Greg” AND address=“320 FL”) (Student)
--The SQL Query Language DML--1 LIKE  LIKE allows to select character strings which have some element in common by using wild cards:  Wild cards:  “%”
Queries and SQL in Access Please use speaker notes for additional information!
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.
Computer Science 101 Web Access to Databases SQL – Extended Form.
Data Manipulation 11 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and other programming.
Relational Query Languages. Languages of DBMS  Data Definition Language DDL  define the schema and storage stored in a Data Dictionary  Data Manipulation.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
Week 10 Quiz 9 Answers Group 28 Christine Hallstrom Deena Phadnis.
CS1100: Microsoft Access Managing Data in Relational Databases Created By Martin Schedlbauer CS11001Microsoft Access - Introduction.
From Relational Algebra to SQL CS 157B Enrique Tang.
Copyright © Curt Hill Queries in SQL More options.
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
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.
Query Processing – Query Trees. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying.
SQL. Sub-query Course Course_nmcredit_hrsstaff_name logic3Smith semantics5Peters programming5 Jones physics3 Hewlett Faculty Faculty_nameNamerank Science.
1 SY306 Web and Databases for Cyber Operations Set #13: SQL SELECT Grouping and sub-queries.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Querying Single Tables with SQL.
QUERY CONSTRUCTION CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
1 Schema for Student Registration System Student Student (Id, Name, Addr, Status) Professor Professor (Id, Name, DeptId) Course Course (DeptId, CrsCode,
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
1 SQL Chapter 9 – 8 th edition With help from Chapter 2 – 10 th edition.
Writing Basic SQL SELECT Statements Lecture
1 Relational Algebra and SQL. 2 Relational Query Languages Languages for describing queries on a relational database Relational AlgebraRelational Algebra.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Structured Query Language IV Asma Ahmad (C. J. Date) Database Systems.
6/22/2018.
Microsoft Access 2003 Illustrated Complete
Writing Basic SQL SELECT Statements
SQL : Query Language Part II CS3431.
(SQL) Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
The Relational Algebra and Relational Calculus
Relational Databases Relational Algebra (1) Select, project, join.
SQL – Entire Select.
Aggregations Various Aggregation Functions GROUP BY HAVING.
Chapter 4 Summary Query.
Aggregating Data Using Group Functions
Access: SQL Participation Project
Dealing with Uniqueness Constraint in Query Optimization
Writing Basic SQL SELECT Statements
Reporting Aggregated Data Using the Group Functions
Chapter Name SQL: Data Manipulation Transparencies JOINS
Reporting Aggregated Data Using the Group Functions
Queries and SQL in Access
Reporting Aggregated Data Using the Group Functions
SQL-Views and External Schemas
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Shelly Cashman: Microsoft Access 2016
04 SQL & Relational Algebra
SQL: The Query Language (Part III)
Group Operations Part IV.
Presentation transcript:

SQL

select * from supplier; Group By   select * from supplier; S_NAME P_No P_NAME CITY PRICE Parsons 23 Part-1 Newcastle 2 R/R 545 Part-2 4 Vickers 342 Part-3 3 Leeds Babcocks 456 Part-6 6 AMEC 213 5 GM Eng. 215 Part-10 Bristol 7 Bae 321 Part-11 GKN York

select * from supplier group by city;   ERROR at line 1:ORA-00979: not a GROUP BY expression select s_name, city from supplier group by city select city from supplier group by city CITY Bristol Leeds Newcastle York   select s_name from supplier group by city; ERROR at line 1:ORA-00979: not a GROUP BY expression

FROM SUPPLIER GROUP BY CITY name part_no part_name city unit_price Parsons 23 Part-1 Newcastle 2.00 R/R 545 Part-2 4.00 Vickers 342 Part-3 3.00   Leeds Babcocks 456 Part-6 6.00 AMEC 213 5.00 G M 215 Part-10 Bristol 7.00 Bae 321 Part-11 GKN York city Newcastle Leeds Bristol York SELECT CITY FROM SUPPLIER GROUP BY CITY

FROM SUPPLIER GROUP BY CITY; name part_no part_name city unit_price   Parsons 23 Part-1 Newcastle 2.00 R/R 545 Part-2 4.00 Vickers 342 Part-3 3.00 Leeds Babcocks 456 Part-6 6.00 AMEC 213 5.00 G M 215 Part-10 Bristol 7.00 Bae 321 Part-11 GKN York SELECT CITY, COUNT(*) FROM SUPPLIER GROUP BY CITY;   city count(*) Newcastle 3 Leeds Bristol 2 York 1  

SELECT CITY, COUNT(*) FROM SUPPLIER WHERE P_NAME='Part-1' S_NAME P_No P_NAME CITY PRICE Parsons 23 Part-1 Newcastle 2 R/R 545 Part-2 4 Vickers 342 Part-3 3 Leeds Babcocks 456 Part-6 6 AMEC 213 5 GM Eng. 215 Part-10 Bristol 7 Bae 321 Part-11 GKN York SELECT CITY, COUNT(*) FROM SUPPLIER WHERE P_NAME='Part-1' GROUP BY CITY;

WHERE... GROUP BY.... name part_no part_name city unit_price Vickers   name part_no part_name city unit_price Vickers 23 Part-1 Leeds 4.00 Parsons Newcastle 2.00 AMEC 213 5.00   GROUP BY.... name part_no part_ city unit_price Vickers 23 Part-1 Leeds 4.00 AMEC 213 5.00 Parsons Newcastle 2.00 city count(*) Leeds 2 Newcastle 1  

Grouping Rule Can only select columns in group by clause. Other columns could contain a different number of values than the grouping column. select * from supplier group by city; Will not work as grouping column city has less values than other columns

HAVING   HAVING is to Groups is what Where is to rows. SELECT CITY,COUNT(*) FROM SUPPLIER WHERE p_name ='Part-1' GROUP BY CITY HAVING COUNT(*)>1; FROM clause produces intermediate table the same as Supplier. WHERE clause evaluated next. GROUP BY clause divides intermediate table into groups based on City values. HAVING clause tests every Group against the condition i.e. count(*)>1. Eliminates any Group not satisfying the condition. SELECT clause projects the chosen columns to produce the final table.   'Provide the names of the cities where there is more than one supplier of part-1, and count the number of suppliers.'

Where... Then...Group By... name part_ no city unit_ price Vickers 342 Newcastle 3.00 R/R 545 Part-2 4.00 GM 215 Part-10 Bristol 7.00 BAe 321 Part-11 23 Part-1 Leeds Babcocks 456 Part-6 6.00 Parsons 2.00 AMEC 213 5.00 GKN York   Where... Then...Group By... name part_ no city unit_ price Parsons 23 Part-1 Newcastle 2.00   Vickers Leeds 4.00 AMEC 213 5.00

Having COUNT(*)>1 Select..... CITY,COUNT(*) name part_ no city   name part_ no city unit_ price Parsons 23 Part-1 Newcastle 2.00   Vickers Leeds 4.00 AMEC 213 5.00 name part_ no city unit_ price Vickers 23 Part-1 Leeds 4.00 AMEC 213 5.00     Select..... CITY,COUNT(*) city count(*) Leeds 2

Summary and Order of Processing   The following are the basic clauses used for SQL queries, in order they are processed in a query- From- defines tables to use and produces Cartesian products in join queries Where- picks rows from tables or Cartesian product based on specified criteria. Can employ sub-queries Group By- arranges rows into groups based on common values in specified column(s) Having- picks out groups satisfying specified criteria, Can apply aggregate functions and use sub-queries Select-picks columns and can apply aggregate functions Distinct-eliminates duplicate values in columns Order By- sorts rows based on specified column values The following set operators are also available for use in Where and Having clauses IN, ALL, ANY, EXISTS