0% found this document useful (0 votes)
67 views27 pages

Architecture Project e ?

The document discusses the typical clauses in an SQL statement used to retrieve data from a database, including SELECT, FROM, WHERE, ORDER BY, GROUP BY, and HAVING. It provides examples of SQL queries using these clauses to select columns, filter rows, sort results, and group data. The examples demonstrate how to use comparison operators, mathematical functions, string functions, and Boolean operators to manipulate and retrieve specific data meeting certain conditions from one or more database tables.

Uploaded by

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

Architecture Project e ?

The document discusses the typical clauses in an SQL statement used to retrieve data from a database, including SELECT, FROM, WHERE, ORDER BY, GROUP BY, and HAVING. It provides examples of SQL queries using these clauses to select columns, filter rows, sort results, and group data. The examples demonstrate how to use comparison operators, mathematical functions, string functions, and Boolean operators to manipulate and retrieve specific data meeting certain conditions from one or more database tables.

Uploaded by

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

Department Computer

CSE Science
of &Engineering

Introduction to SQL - Retrieval

Lecture 15
CSC 401: Database Management System
Typical SQL Statement (DML)

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Clauses of the SELECT Statement
SELECT: Lists the columns (including expressions involving columns) from base tables,

derived tables, or views to be projected into the table that will be the result of the command.
FROM: Identifies the tables, derived tables, or views from which columns will be chosen to

appear in the result table.


WHERE: Includes the conditions for row selection within the items in the FROM clause

and the conditions between tables, derived tables, or views for joining. The WHERE clause
is important in defining the set of rows being manipulated.
ORDER BY: Sorts the final results rows in ascending or descending order.

GROUP BY: Groups rows in an intermediate results table where the values in those rows

are the same for one or more columns.


HAVING: Can only be used following a GROUP BY and acts as a secondary WHERE

clause, returning only those groups that meet a specified condition.


Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Example
Query: Which products have a standard price of less than $275?

SELECT ProductDescription, ProductStandardPrice


FROM Product_T
WHERE ProductStandardPrice < 275;
Result:
PRODUCTDESCRIPTION PRODUCTSTANDARDPRICE

End Table 175

Computer Desk 250

Coffee Table 200

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
DISTINCT and *
If the user does not wish to see duplicate rows in the result, SELECT

DISTINCT may be used. In the preceding example, if the other


computer desk carried by Pine Valley Furniture had also cost less than
$275, the results of the query would have had duplicate rows. SELECT
DISTINCT ProductDescription would display a result table without
the duplicate rows.
SELECT DISTINCT OrderID, OrderedQuantity
FROM OrderLine_T;
SELECT *, where * is used as a wildcard to indicate all columns,

displays all columns from all the items in the FROM clause.
Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Rename AS
 Query: What is the address of the customer named Home Furnishings?

Use an alias, Name, for the customer name.


SELECT CUST.CustomerName AS Name, CUST.CustomerAddress
FROM ownerid.Customer_T AS Cust
WHERE Name = ‘Home Furnishings’;
 Result:
NAME CUSTOMERADDRESS
Home Furnishings 1900 Allard Ave

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Mathematical manipulation
 Query: What are the standard price and standard price if increased by 10 percent for

every product?
SELECT ProductID, ProductStandardPrice, ProductStandardPrice*1.1 AS Plus10Percent
FROM Product_T;
 Result: PRODUCTID PRODUCTSTANDARDPRICE PLUS10PERCENT
2 200 220
3 375 412.5
1 175 192.5
8 250 275
7 800 880
5 325 357
4 650 715
6 750 825

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Using Functions
Mathematical MIN, MAX, COUNT, SUM, ROUND (to round up a number to a
specific number of decimal places), TRUNC (to truncate
insignificant digits), and MOD (for modular arithmetic)

String LOWER (to change to all lower case), UPPER (to change to all
capital letters), INITCAP (to change to only an initial capital
letter), CONCAT (to concatenate), SUBSTR (to isolate certain
character positions), and COALESCE (finding the first not NULL
values in a list of columns)
Date NEXT_DAY (to compute the next date in sequence),
ADD_MONTHS (to compute a date a given number of months
before or after a given date), and MONTHS_BETWEEN (to
compute the number of months between specified dates)

Analytical TOP (find the top n values in a set, e.g., the top 5 customers
by total annual sales)

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Example
Query: What is the average standard price for all products in inventory?

SELECT AVG (ProductStandardPrice) AS AveragePrice FROM Product_T;


Result:

AVERAGEPRICE
440.625
Query: How many different items were ordered on order number 1004?

SELECT COUNT (*) FROM OrderLine_T WHERE OrderID = 1004;


Result:

COUNT (*)
2
Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Need to be careful
Query: How many different items were ordered on order number

1004, and what are they?


SELECT ProductID, COUNT (*)
FROM OrderLine_T
WHERE OrderID = 1004;

Error!!!!!

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
How to overcome
Query: Display for each product the difference between its

standard price and the overall average standard price of all


products.
SELECT ProductStandardPrice – AVG(ProductStandardPrice)
FROM Product_T;
Change to

SELECT ProductStandardPrice – PriceAvg AS Difference


FROM Product_T, (SELECT AVG(ProductStandardPrice) AS
PriceAvg
Department Computer
CSE Science
of FROM Product_T);
&Engineering CSC 401: database Management System Database Group
Examples
Query: Alphabetically, what is the first product name in the

Product table?
SELECT MIN (ProductDescription)
FROM Product_T;

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Comparison Operators
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
!= Not equal to
Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Example
Query: Which orders have been placed since 10/24/2010?
SELECT OrderID, OrderDate
FROM Order_T
WHERE OrderDate > ‘24-OCT-2010’;
Query: What furniture does Pine Valley carry that isn’t made of

cherry?
SELECT ProductDescription, ProductFinish
FROM Product_T
WHERE ProductFinish != ‘Cherry’;

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Boolean Operators
AND: Joins two or more conditions and returns results only

when all conditions are true.


OR: Joins two or more conditions and returns results when any

conditions are true.


NOT: Negates an expression.

Priority order of Boolean operator is AND, OR and NOT

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Example
 Query A: List product name, finish, and standard price for all desks and all tables

that cost more than $300 in the Product table.


SELECT ProductDescription, ProductFinish, ProductStandardPrice
FROM Product_T
WHERE ProductDescription LIKE ‘%Desk’
OR ProductDescription LIKE ‘%Table’
AND ProductStandardPrice > 300;

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Example
 Query B: List product name, finish, and unit price for all desks and

tables in the PRODUCT table that cost more than $300.


SELECT ProductDescription, ProductFinish, ProductStandardPrice
FROM Product_T;
WHERE (ProductDescription LIKE ‘%Desk’
OR ProductDescription LIKE ‘%Table’)
AND ProductStandardPrice > 300;

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Ranges for Qualification
Query: Which products in the Product table have a standard

price between $200 and $300?


SELECT ProductDescription, ProductStandardPrice
FROM Product_T
WHERE ProductStandardPrice > 199 AND ProductStandardPrice <
301;
OR
SELECT ProductDescription, ProductStandardPrice
FROM Product_T
WHERE ProductStandardPrice BETWEEN 200 AND 300;

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
IN and NOT IN with Lists
Query: List all customers who live in warmer states.
SELECT CustomerName, CustomerCity, CustomerState
FROM Customer_T
WHERE CustomerState IN (‘FL’, ‘TX’, ‘CA’, ‘HI’);

Query: List all customers who do not live in warmer states.


SELECT CustomerName, CustomerCity, CustomerState
FROM Customer_T
WHERE CustomerState NOT IN (‘FL’, ‘TX’, ‘CA’, ‘HI’);

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Sorting Results: The ORDER BY Clause
Query: List customer, city, and state for all customers in the

Customer table. List the customers alphabetically by state and


alphabetically by customer within each state.
SELECT CustomerName, CustomerCity, CustomerState
FROM Customer_T
ORDER BY CustomerState, CustomerName;
OR
SELECT CustomerName, CustomerCity, CustomerState
FROM Customer_T
ORDER BY 3, 1;
Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
LIMIT
For cases in which there are many rows in the result table but

you need to see only a few of them, many SQL systems


(including MySQL) support a LIMIT clause, such as the
following, which would show only the first five rows of the
result:
ORDER BY 3, 1 LIMIT 5;

The following would show five rows after skipping the first 30
rows:
ORDER BY 3, 1 LIMIT 30, 5;
Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Categorizing Results: The GROUP BY Clause
GROUP BY is particularly useful when paired with aggregate

functions, such as SUM or COUNT. GROUP BY divides a table


into subsets (by groups); then an aggregate function can be used
to provide summary information for that group.
Scalar aggregate: A single value returned from an SQL query

that includes an aggregate function.


Vector aggregate: Multiple values returned from an SQL query

that includes an aggregate function.

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Example
Query: Count the number of customers with addresses in each

state to which we ship.


SELECT CustomerState, COUNT (CustomerState)
FROM Customer_T
GROUP BY CustomerState;
Query: Count the number of customers with addresses in each

city to which weship. List the cities by state.


SELECT CustomerState, CustomerCity, COUNT (CustomerCity)
FROM Customer_T
GROUP BY CustomerState, CustomerCity;
Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
The HAVING Clause
Query: Find only states with more than one customer.
SELECT CustomerState, COUNT (CustomerState)
FROM Customer_T
GROUP BY CustomerState
HAVING COUNT (CustomerState) > 1;
Query: List, in alphabetical order, the product finish and the average standard price

for each finish for selected finishes having an average standard price less than 750.
SELECT ProductFinish, AVG (ProductStandardPrice)
FROM Product_T
WHERE ProductFinish IN (‘Cherry’, ‘Natural Ash’, ‘Natural Maple’, ‘White Ash’)
GROUP BY ProductFinish
HAVING AVG (ProductStandardPrice) < 750
ORDER BY ProductFinish;
Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
SQL statement processing order
FROM
ORDER BY
Identifies
Sorts rows
involved tables

Result
WHERE
SELECT
Finds all rows
Identifies
meeting stated
columns
condition(s)

GROUP BY HAVING
Organizes rows Finds all groups
according to values meeting stated
in stated column(s) condition(s)

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Thank You

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group

You might also like