Introduction To SQL - 1: Saturday 22,2001
Introduction To SQL - 1: Saturday 22,2001
Introduction to SQL - 1
Saturday 22,2001
Introduction
Official Name
– International Standard Database Language SQL
Sub Languages
– Data Definition Language
• Create
• Alter
• Drop
– Data Manipulation Language
• Select
• Insert
• Update
• Delete
Select Statement
Syntax
SELECT [DISTINCT]{* |column[alias][,..] | groupfunc(column)[,..]}
FROM table[,..]
[WHERE condition(s)]
[GROUP BY {column[,...] }]
[HAVING groupcondition]
[ORDER BY {column | expr | alias [,...]} [ASC|DESC]] ;
– have six main clauses
• select
• from
• where
• group by
• having
• order by
Select Statement
SELECT clause
– used to specify the names of columns to display in output
– * means all columns in the table are selected
– an alias can be asssigned to a column name as:
name [AS] employeename
– column can be a computed column as:
sal * 1.5
– table name can also be used with column names to clarify:
emp.ename
Select Statement
FROM clause
– used to specify the names of tables from which rows to
be selected
– an alias can be used for a table as:
emp e, dept d
– another select statement can also be placed in this
clause
Select Statement
WHERE clause
– used to specify the condition to restrict the rows selected
– a condition given in this clause has three parts:
• column name
• comparison operator
• column name, constant, or list of values
– more than one conditions can be joined using AND, OR and
NOT operators
– a subquery can be used after comparison operator
– using subqueries, be careful about the records returned by
the subquery
• single row comparison operators can only be used with single
row subqueries. ( > , < , >= , <= , <> , =)
• for multiple row subqueries always use multiple row
comparison operaors. (IN , ANY , ALL)
Select Statement
GROUP BY clause
– used to divide rows in a table into groups
– if you include a group function in SELECT caluse then you
cannot include extra columns in SELECT clause unless you
specify all such columns in GROUP BY clause
– column alias can’t be used with GROUP BY clause
– WHERE clause can be used to exclude rows which are not
needed to be grouped
Select Statement
HAVING clause
– WHERE clause can’t be used to restrict groups i.e., group
functions can’t be used in WHERE clause
– used to restrict groups normally using group functions
– you can use subqueries in this clause
– using subqueries, be careful about the records returned by
the subquery
• single row comparison operators can only be used with single
row subqueries. ( > , < , >= , <= , <> , =)
• for multiple row subqueries always use multiple row
comparison operaors. (IN , ANY , ALL)
Select Statement
ORDER BY clause
– used to order/sort the rows returned by the select statement
– an alias or expresssion can be used for sorting purpose
employeename
sal * 1.5
– default sort order is ascending
Select Statement
Some rules
– sub query is executed in first and then its result is applied to
the location where it is used to each row returned by the
main query
– first of all WHERE clause is executed to restrict the rows
– secondly groups are identified if there is a GROUP BY
clause
– then groups are restricted if there is a HAVING clause
– you can’t use ORDER BY clause in sub queries