Академический Документы
Профессиональный Документы
Культура Документы
Some History
SQL is a unified language for defining, querying, modifying and controlling the
data in a relational database
First created by IBM
In 1981, IBM first announced commercial SQL based product , SQL/DS
Several SQL or SQL like products have evolved over last few years
Features of SQL
SELECT Statement
SELECT statement is used to retrieve data from one or more tables (or views)
It includes,
- a ‘SELECT’ clause that lists the columns to retrieve, and
- a ‘FROM’ clause that specifies the tables involved
ORDER BY Clause
1
Should be used as the last clause in SELECT statement
If more than one columns are used, they must be separated by a comma
WHERE Clause
SQL Functions
2
SQL Functions are,
Operate on one row of a table at one Operate on a set of rows at one time
time
Return the same number of rows in the Return a single row for a group of rows
query result in a result
Cannot operate on NULL values unless Operate on null values
a special treatment is given
IIF() function
3
Group Functions
Operate on a set of rows but returns only one (or more if ‘Group’ clause is
used)
GROUP BY Clause
HAVING Clause
1.Display average salary for all departments employing more than three people
4
select deptno, avg(sal) from emp
group by deptno having count (*) > 3;
Example 2-Display only those jobs where the maximum salary is >=3000
Example 1-List the average salary for each job excluding president and sort the
output by salary.
To retrieve data from more than a one table JOINS can be used
To appropriately join tables, the tables must be related and we apply a where
clause which equates the primary key column of the table on the one side of
the relationship with the parallel foreign key column of the many side table.
Joins are : Equi Joins and Non-Equi joins
Equi Join
- Used to retrieve matching rows from two or more tables
- Table headings can be given aliases
- Alias can be upto 30 characters and is defined in FROM clause
- Throughout the SELECT statement the same alias should be used for
that table
5
Select ename, loc from emp , dept
Where emp.deptno = dept.deptno
Find out department name wise total salary for all departments.
Display employee names, their salaries and their grades. (Use EMP and
SALGRADE table.)
Self Join
Which employees have a salary which exceeds the salary of their manager ?
select e.ename, e.mgr, m.ename from emp e, emp m
where e.mgr = m.empno and e.sal > m.sal
**************