Download presentation
Presentation is loading. Please wait.
Published byHilda Bond Modified over 9 years ago
2
Hassan Tariq
3
INTRODUCTION TO SQL What is SQL? –When a user wants to get some information from a database file, he can issue a query. – A query is a user–request to retrieve data or information with a certain condition. –SQL is a query language that allows user to specify the conditions. (instead of algorithms)
4
INTRODUCTION TO SQL Concept of SQL –The user specifies a certain condition. –The result of the query will then be stored in form of a table. –Statistical information of the data. –The program will go through all the records in the database file and select those records that satisfy the condition.(searching).
5
Data Definition Language Data Definition Language Create/alter/drop Data Manipulation Language Data Manipulation Language Insert/delete/updat e Transact-SQL Transact-SQL a sequence of SQL statements
7
FROM 1 WHERE 2 GROUP BY 3 HAVING 4 SELECT 5 ORDER BY 6
8
7 Basic form: (plus many many more bells and whistles) SELECT attributes FROM relations (possibly multiple, joined) WHERE conditions (selections) SELECT attributes FROM relations (possibly multiple, joined) WHERE conditions (selections)
9
8 PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi SELECT * FROM Product WHERE category=‘Gadgets’ Product PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks “selection”
10
9 SELECT Name, Price, Manufacturer FROM Product WHERE Price > 100 Product(PName, Price, Category, Manfacturer) Answer(PName, Price, Manfacturer) Input Schema Output Schema
11
10 What goes in the WHERE clause: x = y, x < y, x <= y, etc For number, they have the usual meanings For CHAR and VARCHAR: lexicographic ordering Expected conversion between CHAR and VARCHAR For dates and times, what you expect... Pattern matching on strings: s LIKE p (next)
12
11 s LIKE p: pattern matching on strings p may contain two special symbols: % = any sequence of characters _ = any single character Product(Name, Price, Category, Manufacturer) Find all products whose name mentions ‘gizmo’: SELECT * FROM Products WHERE PName LIKE ‘%gizmo%’
13
12 PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi SELECT PName, Price, Manufacturer FROM Product WHERE Category IN (‘Photography’,’Household’) Product PNamePriceManufacturer SingleTouch$149.99Canon MultiTouch$203.99Hitachi Use of IN
14
13 PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi SELECT PName, Price, Manufacturer FROM Product WHERE Price BETWEEN 19 and 30 Product PNamePriceManufacturer Gizmo$19.99GizmoWorks Powergizmo$29.99GizmoWorks Use of BETWEEN
15
14 SELECT DISTINCT category FROM Product SELECT DISTINCT category FROM Product Compare to: SELECT category FROM Product SELECT category FROM Product Category Gadgets Photography Household Category Gadgets Photography Household
16
COUNT() Function SELECT COUNT(column_name) FROM table_name SUM() Function SELECT SUM(column_name) FROM table_name MAX() Function SELECT MAX(column_name) FROM table_name MIN() Function SELECT MIN(column_name) FROM table_name
17
AVG() Function SELECT AVG(column_name) FROM table_name IS NULL Clause select * from STUDENT where name is null TOP Clause SELECT TOP 2 * FROM Persons
18
17 SELECT pname, price, manufacturer FROM Product WHERE category=‘gizmo’ AND price > 50 ORDER BY price, pname SELECT pname, price, manufacturer FROM Product WHERE category=‘gizmo’ AND price > 50 ORDER BY price, pname Ordering is ascending, unless you specify the DESC keyword. Ties are broken by the second attribute on the ORDER BY list, etc.
19
18 SELECT Category FROM Product ORDER BY PName SELECT Category FROM Product ORDER BY PName PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi ?
20
GROUPING SELECT...... FROM...... WHERE condition ; GROUP BY groupexpr [HAVING requirement] Group functions: COUNT( ), SUM( ), AVG( ), MAX( ), MIN( ) COUNT( ), SUM( ), AVG( ), MAX( ), MIN( ) –groupexpr specifies the related rows to be grouped as one entry. Usually it is a column. –WHERE condition specifies the condition of individual rows before the rows are group. HAVING requirement specifies the condition involving the whole group.
21
20 PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi SELECT Manufacturer, COUNT(*) as Total FROM Product GROUP BY Manufacturer Product ManufacturerTotal GizmoWorks2 Canon1 Hitachi1 Use of GROUP BY
22
21 PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi SELECT Manufacturer, COUNT(*) as Total FROM Product GROUP BY Manufacturer HAVING COUNT(*)>2 Product ManufacturerTotal GizmoWorks2 Use of GROUP BY with HAVING
23
UNION, INTERSECTION AND DIFFERENCE OF TABLES UNION, INTERSECTION AND DIFFERENCE OF TABLES AB The union of A and B (A B) A table containing all the rows from A and B.
24
UNION, INTERSECTION AND DIFFERENCE OF TABLES UNION, INTERSECTION AND DIFFERENCE OF TABLES The intersection of A and B (A B) A table containing only rows that appear in both A and B. AB
25
UNION, INTERSECTION AND DIFFERENCE OF TABLES UNION, INTERSECTION AND DIFFERENCE OF TABLES The difference of A and B (A–B) A table containing rows that appear in A but not in B. AB
26
25 PNameCategory GizmoGadgets PowergizmoGadgets SELECT PName FROM ProductSET1 UNION SELECT PName FROM ProductSET2 ProductSet1 PName Gizmo Powergizmo MicroUSB UNION PNameCategory MicroUSBGadgets PowergizmoGadgets ProductSet2 SELECT PName FROM ProductSET1 UNION ALL SELECT PName FROM ProductSET2 PName Gizmo Powergizmo MicroUSB Powergizmo UNION ALL
27
26 PNameCategory GizmoGadgets PowergizmoGadgets SELECT PName FROM ProductSET1 INTERSECT SELECT PName FROM ProductSET2 ProductSet1 PName Powergizmo INTERSECT PNameCategory MicroUSBGadgets PowergizmoGadgets ProductSet2
28
BASIC SQL ADVANCED SQL
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.