Вы находитесь на странице: 1из 6

PGDM

STRUCTURED QUERY LANGUAGE

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

 English like language, command set is simple to remember


 Non-procedural language
 SQL Command set can be used by administrators, developers and end-users
 A variety of tasks can be done using SQL statements such as,

 Creating and maintaining database objects


 Querying data stored in the tables
 Controlling the access
 Ensuring database consistency

Few Guidelines for writing an SQL statement

 Use space as a word delimiter


 Command are NOT case-sensitive;

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

 In column list, column alias can be specified


 A column list may contain an expression i.e. a combination of one or more
variables, literal, operators, functions that evaluates a value
 To eliminate duplicate values, DISTINCT clause is used

ORDER BY Clause

 Is used to sort the records in required order

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

SELECT deptno, ename, sal from emp


order by deptno asc, sal desc

WHERE Clause

 It is used to retrieve records based on certain condition/s


 If used, it must follow a FROM Clause
 A condition may contain
- A column name
- A comparison operator (=, <, <=, >, >=, <>)
- An SQL Operator
- BETWEEN … AND
- IN
- IS NULL
- Like (to be used with * or ?)
- All the above operators can be preceded with NOT if negation is
needed

e.g. Select the employees who work as clerks.

Select * from emp where job = ‘CLERK’

1. Select the employees whose salary is between 3000 and 5000.

Select * from emp where sal between 3000 and 5000

2. Select employees in department 10 & 20 in alphabetical order of their names.

Select * from emp where deptno in (10,20) order by ename

3. List all employees having 'LL' in their names.

select ename from emp where ename like '*LL*'

SQL Functions

Typically functions are used to,

 Manipulate numeric and character values


 Convert values of one data type to another data type
 Format data

2
SQL Functions are,

Single Row Functions Group Functions

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

Single Row Functions

Character UCASE, LEN, MID


Numeric ABS, SQR
Date DATE, year,month,
Conversion Cdate,

1. Display name, hiredates who joined in 1982.

Select ename, hiredate from emp where year(hiredate) = 1982

2. Display employees who joined after 12-Jan-82.

Select ename, hiredate from emp where


hiredate > cdate(‘12-Jan-1982’)

IIF() function

 IIF(condition, true statement, false statement)

Example: Get total salary (sal+ comm) of employees. If commission is null it


should be replaced with zero.

SELECT sal, comm, sal+ iif(comm is null,0,comm)


FROM EMP;

3
Group Functions

 Operate on a set of rows but returns only one (or more if ‘Group’ clause is
used)

Select max(sal) from emp;


Select count(*) from emp;

GROUP BY Clause

 Used to create groups of rows in a table


 Group functions are applied when group by clause is used
 WHERE clause can also be used to restrict the input of rows for group
functions.
 If you include an non aggregate attribute (like DEPTNO) in the column list and
do not include it in a group by clause you will get an error because you would
be combining aggregate and detailed data.

Select deptno, avg(sal) from emp group by deptno

Select deptno, avg(sal) from emp where job = ‘CLERK’


group by deptno

select job, count(*) from emp group by job

Select deptno, job, avg(sal) from emp where job = ‘CLERK’


group by deptno, job

HAVING Clause

 It is used to restrict the display of grouped rows


 The result of grouped query is passed on to HAVING clause for output
filtration

EXAMPLES on 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

select job, max(sal) from emp group by job


having max(sal)>=3000

COMPARING 'HAVING' AND 'WHERE' CLAUSE

Example 1-List the average salary for each job excluding president and sort the
output by salary.

select job, avg(sal) from emp where job <> 'president'


group by job order by avg(sal);

Querying Multiple Tables

 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

List employee name and their dept names.

Select ename, dname from emp e, dept d


Where e.deptno = d.deptno

Find locations of all employees

5
Select ename, loc from emp , dept
Where emp.deptno = dept.deptno

Where does ‘Allen’ work ?

Select ename, loc from emp e, dept d


Where e.deptno = d.deptno and e.ename = ‘ALLEN’

Find out department name wise total salary for all departments.

Select d.dname, sum(e.sal) from dept d, emp e


Where d.deptno = e.deptno group by d.dname

 NON EQUI Join

Display employee names, their salaries and their grades. (Use EMP and
SALGRADE table.)

Select ename, grade from emp, salgrade


where sal between losal and hisal

 Self Join

- When you want to compare values within the same table


- For this a table needs to have a relation with itself

Find the manager’s name for all the employees


select e.ename, e.mgr, m.ename from emp e, emp m
where e.mgr = m.empno

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

**************

Вам также может понравиться