Week 9 Updated
Week 9 Updated
WEEK 9
Recap
• using LIKE
SELECT …ORDER BY
• Query results may be sorted using the • SELECT *
ORDER BY clause.
FROM Student
• SELECT *
FROM tablename ORDER BY studentno;
ORDER BY col1; • Will display all the columns in the
*- all attributes table Student in order of
tablename- name of the relation we studentno.
wish to extract data from
Col1- attribute to order the query
results
SELECT … ORDER BY
• ORDER BY clause is used to sort SELECT studentno, Given_Name
the data in ascending or descending FROM Student
order, based on one or more
WHERE Last_Name =“James”
columns.
ORDER BY studentno ASC;
• SELECT columns
• What will be the result of this query
FROM tablename given the Student table displayed
WHERE condition ORDER BY col1, earlier in these slides
col2 ASC|DESC • table
SELECT …GROUP BY
• GROUP BY clause is used in • SELECT col1, col2
collaboration with the SELECT FROM tablename
statement to arrange identical data
into groups. WHERE conditions
• This GROUP BY clause follows GROUP BY col1, col2
the WHERE clause in a SELECT ORDER BY col1 ;
statement and precedes the
ORDER BY clause
SELECT … GROUP BY
• The GROUP BY statement is • There are times you want to
often used with aggregate retrieve results based on
functions (COUNT, MAX, MIN, aggregated functions such as
SUM, AVG). finding the average, finding the
sum, counting the number of
results, finding the minimum and
maximum values.
SELECT… GROUPBY
• SUM sums the values in the collection • SELECT AVG(balance) FROM
• AVG computes average of values in the account WHERE branch_name =
collection 'Perryridge’
• COUNT counts number of elements in
the collection
• SELECT MIN(amount) AS
min_amt, MAX(amount) AS
• MIN returns minimum value in the
collection
max_amt FROM loan;
• MAX returns maximum value in the
collection
AGGREGATE FUNCTIONS
• SELECT COUNT(columnname) • SELECT MIN(columnname)
FROM tablename; FROM tablename;
• SELECT AVG(columnname1) • SELECT MAX(columname)
FROM tablename; FROM tablename;
• SELECT SUM(columnname)
FROM tablename;
HAVING CLAUSE
• The HAVING Clause enables you to • SELECT column_name(s)
specify conditions that filter which group FROM table_name
results appear in the results.
WHERE condition
• HAVING clause places conditions on GROUP BY column_name(s)
groups created by the GROUP BY clause.
HAVING condition
• The HAVING clause must follow the ORDER BY column_name(s);
GROUP BY clause in a query and must
also precede the ORDER BY clause if
used.
SELECT … UNION
• A UNION statement is used to
combine the results of two or more
select statements
• The different select statements must
have the same number of columns,
data types and order
• SELECT column_name(s) FROM ta
ble1
UNION
SELECT column_name(s) FROM ta
ble2;
SELECT … UNION ALL
• A UNION ALL statement is used to
combine the results of two or more
select statements to retrieve
duplicate rows
• SELECT column_name(s) FROM ta
ble1
UNION ALL
SELECT column_name(s) FROM ta
ble2;
ALIAS
• You can rename a table or a column
temporarily by giving another name known
SELECT column_name AS alias_n
as Alias. ame
• The use of table aliases is to rename a table
in a specific SQL statement.
FROM table_name;
• The renaming is a temporary change and the
actual table name does not change in the
database. SELECT column_name(s)
• The column aliases are used to rename a FROM table_name AS alias_name;
table's columns for the purpose of a
particular SQL query.
JOINS
• Joins clause is used to combine records from two or more tables in a database. A JOIN is a means for combining
fields from two tables by using values common to each.
• INNER JOIN is used to select data that have matching records in both tables
• SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
• LEFT JOIN selects data from the left table that have been matched to the right table
• SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
JOINS
• RIGHT JOIN selects data from the right table that have been matched to the left table
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
• FULL OUTER JOIN selects data when there is a match in the left or right table.
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
JOINS
• SELF JOIN is used when you want to join a table to itself
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
Summary
• In this lesson we discussed
• The SELECT command
• Various CLAUSES we can use with SELECT
• How to extract data from more than one relation, joins
Next lecture
• DCL
• Relational Algebra
ANNOUNCEMENT
• Attend your practical classes
• Check that your marks for all your assessments are correctly entered on
ITS.