Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL.

Similar presentations


Presentation on theme: "SQL."— Presentation transcript:

1 SQL

2 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

3 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

4 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

5 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

6 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;

7 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

8 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

9 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.'

10 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

11 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

12 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


Download ppt "SQL."

Similar presentations


Ads by Google