Structured Query Language
Structured Query Language
DDL commands:
CREATE,ALTER,RENAME,DROP.
CREATE:
It is used to create relations (tables)
SYNTAX:
CREATE TABLE table name
( column name data type (size) ,
column name data type(size) [column constraint],
……………
…………..
[table constraint]);
example:
CREATE TABLE sailors
(sid number(3),
sname varchar2(20),
rating number(2),
age number(2),
primary key(sid) );
ALTER:
It is used to alter (modify and add column width and column respectively) the definition
of relation
SYNTAX:
ALTER TABLE table name MODIFY(column name data type (size)…..);
ALTER TABLE table name ADD((new)column name data type (size)…..);
Examples:
ALTER TABLE sailors MODIFY(sid number(4));
ALTER TABLE sailors ADD(mobileno number(10));
RENAME:
It is used to change the name of table
SYNTAX:
RENAME old name TO new name
Example:
RENAME sailors TO sailors_pacific
DROP:
Its is used to remove a table and its whole data
SYNTAX:
DROP TABLE table name
Example:
DROP TABLE sailors_pacific
DML COMMANDS
INSERT:
It is used to insert the values into a relation (table)
SYNTAX:
INSERT INTO table name[(column names)] VALUES(column_values)
Example:
INSERT INTO sailors VALUES(103,’lubber’,8,35);
UPDATE:
Its is used to update a columns value
SYNTAX:
UPDATE table name SET column name=value, SET column name=value
WHERE condition;
Example:
UPDATE sailors SET sname=’rusty’ WHERE sno=103;
DELETE:
It is used to delete the tuples (rows) from a relation(table)
SYNTAX:
DELETE FROM table name WHERE condition
Example:
DELETE FROM sailors WHERE sno=103
SELECT:
It is used to display the data of tables
SYNTAX:
example:
SELECT * FROM sailors;
SELECT sid, rating FROM sailors ;
SELECT sid, rating FROM sailors WHERE sno=103;
NOTNULL:
It specifies that a column cannot contain a ‘null’ values
Example:
CREATE TABLE emp
( empno number(4) NOT NULL,
ename varchar 2(20),……..);
UNIQUE:
It specifies that no two rows in the table can have same value for unique key(i.e. a
column(s) designated as unique)
Example:
CREATE TABLE dept
( deptno number(2),
dname varchar 2(9) CONSTRAINT unq_name UNIQUE,
loc varchar2(10) );
PRIMARY KEY:
It specifies that a column(s) designated as primary key should follow 2 conditions
1. no primary key value can appear in more than one row in the table(unique)
2. no column that is part of the primary key can contain a null(no null)
a table can have only one primary key
SYNTAX:
CREATE TABLE table_name
(col1 datatype(size)[constraint] PRIMARY KEY
……………. );
examples:
CREATE TABLE dept
(deptno number(2) CONSTRAINT pk_dept PRIMARY KEY,
dname varchar2(5),…….);
Example:
CREATE TABLE emp(
Empno number(4),
Ename varchar2(10),
Job varchar2(5),
Mgr number(4),
Hiredate date,
Deptno number(2) CONSTRAINT fk_deptno REFERENCES dept(deptno) ON DELETE
CASCADE );
Because of the ON DELETE CASCADE option any deletion of a deptno value in the
dept table to the deptno values ,then its dependents rows of the emp table also deleted
CHECK:
It specifies a condition on a column
Example:
CREATE TABLE dept
(deptno number (2) CONSTRAINT check_deptno CHECK(deptno BETWEEN 10 AND
99),
dname varchar2(10),……………..);
ORDER BY :
It is used to display the data in an order i.e. ascending or descending
SYNTAX:
ORDER BY column name [asc][desc]
Example:
SELECT * FROM emp ORDER BY salary
SELECT * FROM emp ORDER BY ename ASC ,salary DESC
OPERATORS:
BETWEEN ..AND..
Used to select a range of values
Example:
SELECT * FROM emp WHERE salary BETWEEN 1000 AND 2000
LIKE:
Used to find a pattern
% matches any string of character
_ matches any single character
example:
SELECT * FROM emp WHERE ename LIKE ‘SA%’
SELECT * FROM emp WHERE ename LIKE ‘____’
IS NULL
Used to find the rows whos values is NULL
Example:
SELECT * FROM emp WHERE mgr IS NULL
SET COMPARISION OPERATORS:
IN,ANY,ALL,NOT IN,EXISTS
IN:
It is used to compare any number of operators
Example:
SELECT * FROM emp WHERE job IN (‘CLERK’,’ANALYST’);
SELECT * FROM emp WHERE deptno IN (10,20);
NOT IN :
It is used inverse to the IN operator
ANY:
Compares a value to each value returned by a list or a sub query
Example:
SELECT * FROM emp WHERE sal = ANY (SELECT sal FROM emp WHERE
deptno=30);
ALL:
Compares a value to every(all) values returned by a list or sub query
Example:
SELECT * FROM emp WHERE sal >ALL (SELECT sal FROM emp WHERE
deptno=30);
EXITSTS:
It returns true if the answer of a sub query is contains
SELECT * FROM emp WHERE EXISTS (SELECT sal FROM emp WHERE
deptno=30);
HAVING :
Using this we can pose the conditions on grups .to get the count of employees group by
department no that each department contains atleast 4 employees.
SET OPERATORS:
UNION , INTERSECTION, MINUS(EXCEPT)
We can apply these operators on two or more queries.
These operators works when both the table are union compatible(i.e. no of fileds and data
types of the fields must be same for both tables)
UNION :
It returns all distinct rows returned by both queries
Example:
//list the employees whose salary is equal to that of SCOTT or WARD
Select * from emp where sal in (select sal from emp where ename=’scott’)
UNION
(select sal from emp where ename=’scott’);
//list the employees whose salary is equal to that of SCOTT and WARD
select * from emp where sal in(select sal from emp where enamae=’scott’)
INTERSECTION
(select sal from emp where ename=’ward’);
//list the employees whose salary is equal to the SCOTT but not to WARD
select * from emp where sal in (select sal from emp where ename=’scott’)
MINUS
(select sal from emp where ename=’ward’);
create table sailors(sid number(3),sname varchar2(20),rating
number(2),age number(2), primary key(sid));
7. find the names of sailors who have reserved at least one boat
ans. Select s.sname from sailors s, reserves r where s.sid=r.sid
8. compute the increments for the ratings of persons who have sailed 2 different
boats on the same day
ans. Select s.sname, s.rating+1 as rating from sailors s, reserves r1, reserves r2
where s.sid=r1.sid and s.sid=r2.sid and r1.day=r2.day and r1.bid!=r2.bid;
9. find the ages of sailors whose name begins and ends with B and has at least 3
characters.
Ans. Select age from sailors where sname like ‘B_%B’
10. find the names of sailors who have reserved a red or green boat.
Ans. Select s.sname from sailors s, reserves r, boats b
Where s.sid=r.sid and r.bid=b.bid and b.color=’red’
Union
Select s2.sname from sailors s2, reserves r2, boats b2
Where s2.sid=r2.sid and r2.bid=b2.bid and b2.color=’green’
11. find the names of sailors who have reserved a red and a green boat
ans. Select s.sname from sailors s, reserves r, boats b
Where s.sid=r.sid and r.bid=b.bid and b.color=’red’
Intersection
Select s2.sname from sailors s2, reserves r2, boats b2
Where s2.sid=r2.sid and r2.bid=b2.bid and b2.color=’green’
12. find the names of sailors who have reserved red boats but not green boats
ans. Select s.sname from sailors s, reserves r, boats b
Where s.sid=r.sid and r.bid=b.bid and b.color=’red’
except
Select s2.sname from sailors s2, reserves r2, boats b2
Where s2.sid=r2.sid and r2.bid=b2.bid and b2.color=’green’
13. find all sids of sailors who have a rating 10 or reserved boats 104.
Ans. Select sid from sailors where rating = 10
Union select sid from reserves where bid=104;
NESTED QUERIES :
14. find the names of sailors who have reserved boat 103
ans. Select s.sname from sailors s where s.sid in (select r.sid from reserves r where
r.bid=103)
15. find the names f sailors who have reserved a red boat
ans. Select s.sname from sailors s where in
(select r.sid from reserves r where r.bid in
(select bid from boats b where b.color=’red’);
16. find the names of sailors who have not reserved a red boat
ans. Select s.sname from sailors s where s.sid not in
(select r.sid from reserves r where r.bid in
(select bid from boats b where b.color=’red’));
17. find the names of sailors who have reserved boat no 103
ans. Select s.sname from sailors s where exists (select *from
reserves r where r.bid=103 and r.sid=s.sid);
18.find the sailors whose rating is better than some sailor called horatio
ans. Select s.sid from sailors s where s.rating > any ( select s2.rating
from sailors s2 where s2.sname=’horatio’);
20. find the names of sailors who have reserved both a red and green boat
ans. Select s.sname from sailors s, reserves r, boats b
where s.sid=r.sid and r.bid=b.bid and b.color=’red’ and s.sid in(select s2.sid
from sailors s2, reserves r2, boats b2 where s2.sid=r2.sid and r2.bid=b2.bid and
b2.color=’green’)
21. find the names of sailors who have reserved all boats
ans. Select s.sname from sailors s where not exists((select b.bid from boats b) minus
(select r.bid from reserves r where r.sid=s.sid))
AGGREGATE OPERATORS
27. find the names of sailors who are older than the oldest sailor with a rating of 10
ans. Select s.sname from sailors s where s.age> ( select max(s2.age) from sailors s2
where s2.rating = 10);
select s.sname from sailors s where s.age > all (select s2.age from sailors s2
where s2.rating = 10);
GROUP BY and HAVING clauses
28. find the age of the youngest sailor for each rating level
ans. Select rating , min(age) from sailors group by rating
29. find the age of the youngest sailor who is eligible to vote(18 years old) for each
rating level with al least 2 such sailors
ans. Select rating, min(age) minage from sailors
where age >= 18 group by rating having count(*) > 1;