Architecture Project e ?
Architecture Project e ?
CSE Science
of &Engineering
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
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
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
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?
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?
AVERAGEPRICE
440.625
Query: How many different items were ordered on order number 1004?
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
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
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
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
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
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
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’);
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
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
Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Example
Query: Count the number of customers with addresses in each
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