0% found this document useful (0 votes)
2 views31 pages

SQL Chapter 2

Uploaded by

advishe494
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views31 pages

SQL Chapter 2

Uploaded by

advishe494
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

SQL Chapter 3

By Prof Yogita V Deshmukh


Faculty Training and Placement VNIT Nagpur

By Prof Yogita V Deshmukh


The SQL SELECT Statement

By Prof Yogita V Deshmukh


Select ALL columns

By Prof Yogita V Deshmukh


The SQL SELECT DISTINCT Statement

By Prof Yogita V Deshmukh


Output :

By Prof Yogita V Deshmukh


Count Distinct

By Prof Yogita V Deshmukh


Example :

By Prof Yogita V Deshmukh


The SQL WHERE Clause

By Prof Yogita V Deshmukh


SQL Query

Basic form: (plus many many more bells and whistles)

SELECT <attributes>
FROM <one or more relations>
WHERE <conditions>

By Prof Yogita V Deshmukh


Simple SQL Query
Product PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi

SELECT *
FROM Product
WHERE category=‘Gadgets’

PName Price Category Manufacturer


Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
“selection”
By Prof Yogita V Deshmukh
Simple SQL Query
Product PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi

SELECT PName, Price, Manufacturer


FROM Product
WHERE Price > 100

PName Price Manufacturer


“selection” and SingleTouch $149.99 Canon
“projection” MultiTouch $203.99 Hitachi

By Prof Yogita V Deshmukh


Notation
Input Schema

Product(PName, Price, Category, Manfacturer)

SELECT PName, Price, Manufacturer


FROM Product
WHERE Price > 100

Answer(PName, Price, Manfacturer)

Output Schema
By Prof Yogita V Deshmukh
The SQL ORDER BY

By Prof Yogita V Deshmukh


DESC Keyword

By Prof Yogita V Deshmukh


Order-By Alphabetically

By Prof Yogita V Deshmukh


Alphabetically DESC

By Prof Yogita V Deshmukh


Using Both ASC and DESC

By Prof Yogita V Deshmukh


The SQL IN Operator

By Prof Yogita V Deshmukh


The SQL BETWEEN Operator

By Prof Yogita V Deshmukh


SQL Aliases

By Prof Yogita V Deshmukh


SQL Aggregate Functions

By Prof Yogita V Deshmukh


The SQL MIN() and MAX() Functions

By Prof Yogita V Deshmukh


Max Example

By Prof Yogita V Deshmukh


SQL SUM() Function

By Prof Yogita V Deshmukh


The SQL AVG() Function

By Prof Yogita V Deshmukh


The SQL LIKE Operator

By Prof Yogita V Deshmukh


Details
• Case insensitive:
• Same: SELECT Select select
• Same: Product product
• Different: ‘Seattle’ ‘seattle’

• Constants:
• ‘abc’ - yes
• “abc” - no

By Prof Yogita V Deshmukh


The LIKE operator
SELECT *
FROM Products
WHERE PName LIKE ‘%gizmo%’

• s LIKE p: pattern matching on strings


• p may contain two special symbols:
• % = any sequence of characters
• _ = any single character

By Prof Yogita V Deshmukh


Eliminating Duplicates
Category
SELECT DISTINCT category Gadgets
FROM Product Photography
Household

Compare to:
Category
Gadgets
SELECT category Gadgets
FROM Product Photography
Household

By Prof Yogita V Deshmukh


Ordering the Results

SELECT pname, price, manufacturer


FROM Product
WHERE category=‘gizmo’ AND price > 50
ORDER BY price, pname

Ties are broken by the second attribute on the ORDER BY list, etc.

Ordering is ascending, unless you specify the DESC keyword.

By Prof Yogita V Deshmukh


PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi

SELECT DISTINCT category


FROM Product
ORDER BY category ?
SELECT Category
FROM Product
ORDER BY PName
?
SELECT DISTINCT category
FROM Product
ORDER BY PName By Prof Yogita V Deshmukh
?

You might also like