Module 3
Module 3
– Ai represents an attribute
– Ri represents a relation
– P is a predicate.
• The result of an SQL query is a relation.
• The basic structure of SQL expression consist of three clauses
– Select clause
– From clause
– Where clause
The select Clause
• The select clause lists the attributes desired in the result of a
query
– corresponds to the projection operation of the relational
algebra
• Example: find the names of all instructors:
select name
from instructor
• NOTE: SQL names are case insensitive (i.e., you may use
upper- or lower-case letters.)
– E.g., Name = NAME = name
The select Clause
SELECT name
from`instructor`
The select Clause
• SQL allows duplicates in relations as well as in query results.
• To force the elimination of duplicates, insert the keyword distinct
after select.
• Find the department names of all instructors, and remove
duplicates
select distinct dept_name
from instructor
• The keyword all specifies that duplicates should not be removed.
SELECT `ID`,`name`,`salary`/12
FROM `instructor` WHERE 1
The where Clause
• The where clause specifies conditions that the result must satisfy
– Corresponds to the selection predicate of the relational algebra.
• To find all instructors in Comp. Sci. dept
select name
from instructor
where dept_name = ‘Comp. Sci.'
• Comparison results can be combined using the logical connectives and, or, and
not
– To find all instructors in Comp. Sci. dept with salary > 80000
select name
from instructor
where dept_name = ‘Comp. Sci.' and salary > 80000
• Comparisons can be applied to results of arithmetic expressions.
The where Clause
– UNION
– UNION ALL
– INTERSECT
– MINUS
Union
• The SQL Union operation is used to combine the result of two or more
SQL SELECT queries.
• In the union operation, all the number of datatype and columns must be
same in both the tables on which UNION operation is being applied.
• The union operation eliminates the duplicate rows from its resultset.
• Syntax
SELECT column_name FROM table1
UNION
SELECT column_name FROM table2;
ID NAME ID NAME
1 JACK 3 JACKSON
2 HARRY 4 STEPHAN
3 JACKSON 5 DAVID
2 HARRY
3 JACKSON
4 STEPHAN
5 DAVID
Union All
ID NAME
1 JACK
2 HARRY
3 JACKSON
3 JACKSON
4 STEPHAN
5 DAVID
Intersect
ID NAME
3 JACKSON
Minus
ID NAME
1 JACK
2 HARRY
SQL JOIN
► How do I get data from multiple tables?
► A SQL JOIN combines records from two tables.
► Then, any matched records from the second table (right-most) will be
included.
► LEFT JOIN and LEFT OUTER JOIN are the same.
The SQL LEFT JOIN syntax
► The general LEFT OUTER JOIN syntax is
SELECT column-names
FROM table-name1 LEFT OUTER JOIN table-name2
ON column-name1 = column-name2
WHERE condition
SELECT column-names
FROM table-name1 RIGHT OUTER JOIN table-name2
ON column-name1 = column-name2
WHERE condition
► These two: FULL JOIN and FULL OUTER JOIN are the same.
The SQL FULL JOIN syntax
► The general FULL OUTER JOIN syntax is:
SELECT column-names
FROM table-name1 FULL OUTER JOIN table-name2
ON column-name1 = column-name2
WHERE condition
► The most commonly used SQL aggregate functions include SUM, MAX,
MIN, COUNT and AVERAGE.
► Aggregators are very often used in conjunction with Grouping functions in
order to summarize the data.
SQL COUNT()
► The COUNT() function returns the number of rows that matches a specified
criteria.
► SQL COUNT(column_name)
counts the number of orders from "CustomerID"=7 from the "Orders" table:
SELECT COUNT(CustomerID) AS
OrdersFromCustomerID7 FROM Orders
WHERE CustomerID=7;
OrdersFromCustomerID7
1
SQL COUNT(*) Example
OrderID CustomerID EmployeeID OrderDate ShipperID
10265 7 2 1996-07-25 1
10266 87 3 1996-07-26 3
10267 25 4 1996-07-29 1
To get number of rows in the 'orders' table, the following SQL statement can be
used:
SELECT COUNT(*)
FROM orders;
COUNT(*)
3
SQL COUNT(DISTINCT column_name) Example
OrderID CustomerID EmployeeID OrderDate ShipperID
10265 7 2 1996-07-25 1
10266 87 3 1996-07-26 3
10267 25 4 1996-07-29 1
COUNT(DISTINCT ShipperID)
2
SQL SUM, AVG
► SELECT SUM returns the sum of the data values.
► And SELECT AVG returns the average of the data values.
► The general SUM syntax is:
SELECT SUM(column-name)
FROM table-name;
► The general AVG syntax is:
SELECT AVG(column-name)
FROM table-name;
SQL SUM, AVG Examples
► Problem: Compute the total amount sold in 2013.
SELECT SUM(TotalAmount) Sum
Eg select all customers from the customers table, sorted by the country column
Query: select * from customer order by country; auto ascending
POSITION MIN(SALARY)
MANAGER 24000
PROJECT 9000
MANAGER
HAVING Clause
► Both WHERE and HAVING can be used in the same query at the same
time.
• Eg: select the name of the employee held in various positions whose salary
is above 10,000
• Query: Select position, fname from staff group by(position) having
salary>10000
POSITION FNAME
MANAGER JOHN
PROJECT MANAGER DAVID
► Problem: List the number of customers in each country. Only include countries with
more than 10 customers.
SELECT COUNT(Id), Country
FROM Customer
GROUP BY Country
HAVING COUNT(Id) > 10
Problem: List the number of customers in each country, except the USA, sorted
high to low. Only include countries with 9 or more customers.
SELECT
COUNT(Id), Country
FROM Customer
WHERE Country <> 'USA'
GROUP BY Country where count(id) is not possible
a.UPDATE ALBUMS
SET YEAR = 2018
WHERE ALBUM-NAME = 'suhana';
b. DELETE FROM ALBUMS
WHERE ALBUM-NAME = 'YADON KI BAARISH';