You are on page 1of 31

More SQL commands

WEEK 9
Recap

• Data Manipulation Language (DML) is the part of SQL used to manipulate


data within objects of a relational database.
• it is used to store, modify, retrieve, delete and update data in a database
• The most basic DML commands:
• INSERT - insert data into a table
• UPDATE - updates existing data within a table
• DELETE - Delete all records from a database table
2
Recap
INSERT INTO TABLE_NAME     • The INSERT statement is used to insert data into a
row in a table, use one of these
(col1, col2, col3,.... col N)   • The list of values must match the table structure
VALUES (value1, value2, value3, .... val exactly in the number of attributes and the data
type of each attribute.
ueN); 
• Tablename- is the table you want to insert data
into

INSERT INTO TABLE_NAME     • Columnname- name of column as defined in the


table
VALUES (value1, value2, value3, .... val • Values- the value you want to insert into that
ueN);  column
Objectives
By the end of this lesson, students should be able to :
• Use the SELECT command
• Use various clauses with SELECT
• Extract data from various relations
Student Relation
• Consider this relation for illustrations in this lesson
Student
studentno Given_Name Last_Name
22226789 Tiffany Andreas
22345678 John James
23478961 Susan Peters
24571234 Joseph James
SELECT
• To retrieve data from the database we can use the SELECT command
• SELECT will retrieve information from the database that matches the specified
criteria using the SELECT/FROM/WHERE framework.
SELECT
FROM
WHERE
• When you want to retrieve data based on certain criteria, the WHERE clause comes
in handy allowing you to specify the condition.
SELECT
• A query pulls information from one or more relations and creates (temporarily)
a new relation.
• A SQL query is a “select-from-where” expression
• Nested subqueries are “select-from-where” expressions embedded within
another quer
• This allows a query to:
• Create a new relation
• Feed information to another query (as a “sub-query”)
SELECT …WHERE
SELECT col1,col2,…coln • SELECT studentno, Given_name
FROM tablename; FROM Student;
SELECT col1,col2,…coln SELECT studentno, Given_name
FROM tablename FROM Student
WHERE condition; WHERE studentno = 12345678;
SELECT Statement
• To show all of the column values • What do you think will be the
for the rows that match the result of executing this query on
specified criteria, use an asterisk our college database?
( * ).
SELECT *
SELECT *
FROM tablename;
FROM Student;
SELECT
•  subquery is used to return data that will be used in the main query as a condition to further
restrict the data to be retrieved.
• Subqueries must be enclosed within parentheses.
• A subquery can have only one column in the SELECT clause, unless multiple columns are in the
main query for the subquery to compare its selected columns.
• An ORDER BY command cannot be used in a subquery, although the main query can use an
ORDER BY. The GROUP BY command can be used to perform the same function as the
ORDER BY in a subquery.
• Subqueries that return more than one row can only be used with multiple value operators such as
the IN operator
SELECT … DISTINCT
• DISTINCT • There are times you want to
• The DISTINCT statement is used retrieve results based on
to remove duplication in the aggregated functions such as
results retrieved finding the average, finding the
sum, counting the number of
• SELECT DISTINCT
column_name(s)
results, finding the minimum and
FROM table_name maximum values.
WHERE condition;
SELECT Statement
• The WHERE clause may include SELECT Given_Name
the IN keyword to specify that a
particular column value must be FROM Student
included in a list of values. WHERE studentno
SELECT columnname IN
FROM tablename (22226789,12345678)
WHERE column IN ;
(values1,value2,value3);
SELECT Statement
• SQL provides a BETWEEN keyword SELECT Given_Name
that allows a user to specify a minimum
and maximum value on one line. FROM Student
SELECT column
WHERE TestMark
FROM tablename BETWEEN 50 AND 60;
WHERE column
BETWEEN value1 AND
value2;
SELECT Statement

• The LIKE operator is used in a WHERE clause to search for a specified


pattern in a column.
• There are two wildcards often used in conjunction with
the LIKE operator:
• The percent sign (%) represents zero, one, or multiple characters
• The underscore sign (_) represents one, single character
SELECT … LIKE
• SELECT column • SELECT studentno
• FROM tablename • FROM Student
• WHERE column LIKE • WHERE Given_Name LIKE ‘a
‘character/s%’; %';

• 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.

You might also like