You are on page 1of 17

Sql Divide into 5 sub languages 1)Data Defination Language: Used to define(make,change,remove)data base objects.

Database Object-A component permenantly stored in server. 1)Create 2)Alter 3)Drop Create: Creating the table Syntax: Create Table <Table Name> (Column1 <data_type>, Column2 <data_type>, . . Column N <data_type>); Example: Create Table Studentcse( Stud_no number(9), Stud_name varchar2(9), Stud_addr varchar2(9), Stud_fee number(9)); ------------Table Created--------If we see table structure ,then we used the command -Describe/desc Syntax: Desc Student Stud_no number(9) Stud_name varchar2(9) Stud_addr varchar2(9) 2)Alter: Used to change Struture of table 3 options-- 1)Add 2)modify 3)drop 1)Add: used to add new columns Example: alter table student add(phone number(15),mail_id varchar2(9)); ---------Table Altered--------------2)modify:used to change existing column size/datatype Example:alter table student modify stud_no number(20). 3)drop:used to remove a column. Example:alter table student drop column Stud_addr; Alter table student drop(Stud_no,Stud_name); 3)Drop: Used to remove database Objects Syntax: Drop Table <Table name> Example: Drop table Student

Data manipulation Language: Used to manipulate information in database objects. 1)Insert: Used to feed new rows into database objects Syntax1: insert into <Table_name> values(list of values); Example:insert into studentcse values(10,venkat,hyd); insert into student values(20,Ram,mumbai); Syntax2:Supports to insert selected columns) insert into <table_name>(columnlist)values(list of values); Example: insert into student(Stud_no,Stud_addr)values(20,chennai); Syntax3 suppose we want to insert multiple rows at a time insert into dept values(&deptno,'&dname',&loc); - rest of columns are filled with empty values(Null values) Null keyword used to insert empty value into table column supports all datatypes. Conditional Clause -used to specify condition while retrieving/manipulating data. WHERE clause used with update,delete & select statements 2)Update: Used to change the existing information in table. Syntax: update <Table name> set column1=value,column2=value, -----where <condition>]; Examples:update student set fee=fee+1000; Update dept set loc=MUMBAI where deptno=20; Update emp set deptno=20,hiredate=20-may-07 where empno=1002; 3)Delete: Used to remove rows from table. Syntax: delete from <TableName>[where <condition>]; Example:delete from student; Delete from emp; Delete from emp where empno=1007; Delete from dept where deptno=20; Data Querying/Retrival Language(DQL/DRL) Used to retrive the information from database objects for read only purpose. --SELECT Statement. Syntax: Select <column list> from <Table Name> [Where <condition> Group by <columns>

Having<condition> Order by<columns>]; Examples:select empno,ename,sal,deptno from emp; Select stud_no,stud_name,stud_fee from student; Select * from emp; Select * from dept; Select * from tab;---gives list of tables in user login *----represents all columns of table. Examples: Select * from emp [ * ] where ename = ram where deptno=20 and job=analyst where job <> manager where sal >=4000 and sal<=9000 where hiredate>=1-jan-05 and hiredate<=31-dec-05 Data Control Language: It control flow of data between users The commands are 1)Grant: It give the permissions to users 2)Revoke:To cancel the permissions from users Transaction Control Language: Used to control Dml operations 1)Commit:saves the changes permanently. 2)Rollback:undo operations taken place. GROUP BY CLAUSE --used the group the rows based on specified coloumn. --whenever ordinary columns retrived along with aggregate functions,all the Ordinary coloumns are to be specified after group by clause. Example: 1)Write a query to display the department wise sum of salaries SQL> select * from emp; HIREDATE --------17-DEC-80 20-FEB-81

EMPNO ENAME JOB MGR SAL COMM DEPTNO ---------- ---------- --------- ------------------- ---------- ---------7369 SMITH CLERK 7902 800 20 7499 ALLEN SALESMAN 7698 1600 300 30

1250 2975 1250 2850

7521 WARD 500 7566 JONES 7654 MARTIN 1400 7698 BLAKE 7782 CLARK

2450 7788 SCOTT 3000 7839 KING 5000 1500 1100 7900 JAMES 950 7902 FORD 3000 7934 MILLER 1300 14 rows selected. 7844 TURNER 0 7876 ADAMS

SALESMAN 30 MANAGER 20 SALESMAN 30 MANAGER 30 MANAGER 10 ANALYST 20 PRESIDENT 10 SALESMAN 30 CLERK 20 CLERK 30 ANALYST 20 CLERK 10

7698 22-FEB-81 7839 02-APR-81 7698 28-SEP-81 7839 01-MAY-81 7839 09-JUN-81 7566 19-APR-87 17-NOV-81 7698 08-SEP-81 7788 23-MAY-87 7698 03-DEC-81 7566 03-DEC-81 7782 23-JAN-82

SQL> select deptno,sum(sal) from emp group by deptno; DEPTNO SUM(SAL) ---------- ---------10 8750 20 10875 30 9400 HAVING --used to specify condition on grouped results. --used only after group by. --with out group by it is not valid. Example: 1)Write a query to display department wise sum of salaries,having greater than 9000. SQL> select deptno,sum(sal) from emp group by deptno having sum(sal) > 9000; DEPTNO SUM(SAL)

---------- ---------20 10875 30 9400 2)Write a query to display total no of jobs,having employees more than one. SQL> select job,count(*) from emp group by job having count(*)>1; JOB COUNT(*) --------- ---------ANALYST 2 CLERK 4 MANAGER 3 SALESMAN 4 Order by --used to arrange the output of select statement in ascending(default) or decending Order. Asc---ascending,,,desc descending. SQL> select * from emp order by sal; EMPNO ENAME JOB MGR SAL COMM DEPTNO ---------- ---------- --------- ------------------- ---------- ---------7369 SMITH CLERK 7902 800 20 7900 JAMES CLERK 7698 950 30 7876 ADAMS CLERK 7788 1100 20 7521 WARD SALESMAN 7698 1250 500 30 7654 MARTIN SALESMAN 7698 1250 1400 30 7934 MILLER CLERK 7782 1300 10 7844 TURNER SALESMAN 7698 1500 0 30 7499 ALLEN SALESMAN 7698 1600 300 30 7782 CLARK MANAGER 7839 2450 10 7698 BLAKE MANAGER 7839 2850 30 HIREDATE --------17-DEC-80 03-DEC-81 23-MAY-87 22-FEB-81 28-SEP-81 23-JAN-82 08-SEP-81 20-FEB-81 09-JUN-81 01-MAY-81

7566 JONES 2975 7788 SCOTT 3000 7902 FORD 3000 7839 KING 5000 14 rows selected.

MANAGER 20 ANALYST 20 ANALYST 20 PRESIDENT 10

7839 02-APR-81 7566 19-APR-87 7566 03-DEC-81 17-NOV-81

SQL> select * from emp order by sal desc; EMPNO ENAME JOB MGR SAL COMM DEPTNO ---------- ---------- --------- ------------------- ---------- ---------7839 KING PRESIDENT 5000 10 7788 SCOTT ANALYST 7566 3000 20 7902 FORD ANALYST 7566 3000 20 7566 JONES MANAGER 7839 2975 20 7698 BLAKE MANAGER 7839 2850 30 7782 CLARK MANAGER 7839 2450 10 7499 ALLEN SALESMAN 7698 1600 300 30 7844 TURNER SALESMAN 7698 1500 0 30 7934 MILLER CLERK 7782 1300 10 7521 WARD SALESMAN 7698 1250 500 30 7654 MARTIN SALESMAN 7698 1250 1400 30 7876 ADAMS CLERK 7788 1100 20 7900 JAMES CLERK 7698 950 30 7369 SMITH CLERK 7902 800 20 14 rows selected. HIREDATE --------17-NOV-81 19-APR-87 03-DEC-81 02-APR-81 01-MAY-81 09-JUN-81 20-FEB-81 08-SEP-81 23-JAN-82 22-FEB-81 28-SEP-81 23-MAY-87 03-DEC-81 17-DEC-80

DISTINCT Clause --used to suppress duplicate values in select statement output. SQL> SELECT JOB FROM EMP; JOB --------CLERK SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST PRESIDENT SALESMAN CLERK CLERK ANALYST CLERK SQL> SELECT DISTINCT(JOB) FROM EMP; JOB --------ANALYST CLERK MANAGER PRESIDENT SALESMAN

CONSTRAINTS --A set of predefined rules applied on table columns.while creating tables or after Creation. --these are automatically activated whenever dml statements are performed on tables. --these are activated when tables are manipulated by other users and other Application software tools. --they provide security. 3 types of constraints.

1)Domain Integrity rules:-checks for uniqueness of data. Ex:-check,notnull check:-used to specify conditional restriction on columns. Notnull:-used to suppress null values. 2)Entity Integrity rules:-checks for conditional restriction on tables. Ex:-primary key,unique Primary key:-not null + unique + index. --it used to define key column of table. --it will not accept null values and duplicate values. --it can be used only once in table definition. Index:-it is a pointer locates the physical address of data. --it is used to improve performance while retriving or manipulating data through primary key. --it is automatically activated whenever primary key is used in where clause. Unique:-used to suppress duplicate values But it accepts null values. 3)Refrential Integrity rules:-supports to establish relation between 2 entities. Ex:-references. --used to define relation between 2 tables. --it is known as foreign key. --it accepts null values and duplicates. --it can be linked with a primary key column or unique constraint column Of other table. 2 types of syntax: Column constraint syntax: --used to define constraints next to column definition. Table constraint syntax: --used to define constraints at the end of table definition. --supports to define composite primary key and composite Foreign key. --not null and default are not allowed in this syntax. --supports to define constraints on existing table also. Column constraint syntax: 1)Create table dept_2(deptno number(2) primary key, Dname varchar2(20) not null unique, Loc varchar2(20) default hyderabad); Insert into dept_2 values (10,sales,hyderabad); (null,export,mumbai); (10,software,hitech); (20,null,secbad); (30,sales,mumbai); (40,sales,pune);

Default:-keyword(8.0) --used to accept default value into column. Example: 1 Create table dept_2(deptno number(2) primary key,Dname varchar2(20) not null unique,Loc varchar2(20) default'hyderabad'); Table created. SQL> desc dept_2; Name Null? Type DEPTNO NOT NULL NUMBER(2) DNAME NOT NULL VARCHAR2(20) LOC VARCHAR2(20) SQL> Insert into dept_2 values 1 row created. SQL> insert into dept_2 values(null,'export','chennai'); insert into dept_2 values(null,'export','chennai') * ERROR at line 1: ORA-01400: cannot insert NULL into ("SCOTT"."DEPT_2"."DEPTNO") SQL> insert into dept_2 values(10,'software','mumbai'); insert into dept_2 values(10,'software','mumbai') * ERROR at line 1: ORA-00001: unique constraint (SCOTT.SYS_C002719) violated SQL> insert into dept_2 values(30,'sales','mumbai'); insert into dept_2 values(30,'sales','mumbai') * ERROR at line 1: ORA-00001: unique constraint (SCOTT.SYS_C002720) violated SQL> insert into dept_2 values(30,'export','mumbai'); (10,'sales','hyderabad');

1 row created. SQL> select * from dept_2; DEPTNO DNAME LOC ---------- -------------------- -------------------10 sales hyderabad 30 export mumbai 2)Create table emp_2(empno number(4) primary key, Ename varchar2(20) not null, Sal number(12,2)check(sal > =2000), Sex char(1) check(sex in (M,F)), Hiredate date default sysdate, Deptno number(2) references dept_2(deptno));--Foreighn key SQL> Create table emp_2(empno number(4) primary key, Ename varchar2(20) not null, Sal number(12,2) check(sal > =2000), Sex char(1) check(sex in ('M','F')), Hiredate date default sysdate, Deptno number(2) references dept_2(deptno)); Table created. SQL> insert into emp_2 values(100,'RAM',2500,'M',sysdate,10); 1 row created. SQL> insert into emp_2 values(300,'sita',1400,'F',sysdate,20); insert into emp_2 values(300,'sita',1400,'F',sysdate,20) * ERROR at line 1: ORA-02290: check constraint (SCOTT.SYS_C002722) violated SQL> select * from emp_2; EMPNO ENAME SAL S HIREDATE DEPTNO ---------- -------------------- ---------- - -----------------100 RAM 2500 M 23-JAN-10 10

Subqueries or Nested queries -->query with in a query is known as subquery. -->first inner query will be excueted & based on result of inner query ,outer query will be executed. 1)List all the Employees who are working in SMITHs Department. SQL> select * from emp where deptno in(select deptno from emp where ename = 'SMITH'); EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7369 SMITH CLERK 7902 17-DEC-80 800 7876 ADAMS CLERK 7788 23-MAY-87 1100 7902 FORD ANALYST 7566 03-DEC-81 3000 7788 SCOTT ANALYST 7566 19-APR-87 3000 7566 JONES MANAGER 7839 02-APR-81 2975 2)List all the Employees who are having Highest Salary? SQL> select * from emp where sal=(select max(sal) from emp); EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7839 KING PRESIDENT 17-NOV-81 5000 3)List all the Employees who are having Subordinates? SQL> select * from emp where empno in(select distinct mgr from emp); EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7566 JONES MANAGER 7839 02-APR-81 2975 7698 BLAKE MANAGER 7839 01-MAY-81 2850 7788 SCOTT ANALYST 7566 19-APR-87 3000 7839 KING PRESIDENT 17-NOV-81 5000 7902 FORD ANALYST 7566 03-DEC-81 3000 Special operators in subqueries COMM 20 30 20 10 20 COMM 10 COMM 20 20 20 20 20

1)EXISTS:returns TRUE/FALSEgives status of inner query. 1)List the Employees of 10 dept if there are more than 5 Analysts in the same dept. SQL> select * from emp where deptno=30 and EXISTS(select count(*) from emp where deptno=30 2 and job='SALESMAN' group by job having count(*)>3); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7900 JAMES CLERK 7698 03-DEC-81 950 30 6 rows selected. 2)ANY:-picks smallest value from result of inner query. 1)List all the Employees whose salary is more than lowest salary of 10th deptamnt. SQL> select * from emp where sal>any(select sal from emp where deptno=10); EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7566 JONES MANAGER 7839 02-APR-81 2975 7698 BLAKE MANAGER 7839 01-MAY-81 2850 7788 SCOTT ANALYST 7566 19-APR-87 3000 7839 KING PRESIDENT 17-NOV-81 5000 7902 FORD ANALYST 7566 03-DEC-81 3000 COMM 20 30 20 10 20

2)List the employees details of those employees who salary is greater than any of the managers. SQL> select * from emp where sal > any (select sal from emp where job ='MANAGER'); EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7566 JONES MANAGER 7839 02-APR-81 2975 7698 BLAKE MANAGER 7839 01-MAY-81 2850 7788 SCOTT ANALYST 7566 19-APR-87 3000 7839 KING PRESIDENT 17-NOV-81 5000 COMM 20 30 20 10

7902 FORD

ANALYST

7566 03-DEC-81

3000

20

SQL> select sal from emp where job ='MANAGER'; SAL ---------2975 2850 2450 3)ALL:-picks highest value from result of inner query. 1)list the employee earning more than highest paid Manager SQL> select * from emp where sal > all(select sal from emp where job ='MANAGER'); EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7788 SCOTT ANALYST 7566 19-APR-87 3000 7839 KING PRESIDENT 17-NOV-81 5000 7902 FORD ANALYST 7566 03-DEC-81 3000 COMM 20 10 20

2)list the employee names who salary is greater than the highest salary of all employee belonging to department 20. SQL> select * from emp where sal > all (select sal from emp where deptno=20); EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7839 KING PRESIDENT 17-NOV-81 5000 COMM 10

Correlated subquery In a correlatedsubquery --first outer query will be excuted and based on the output of outer query,inner query will be excueted. --column from outer query will be substituted in inner query condition through table alias name,it is known as correlated column. 1)list the employees whose salary is more than their dept average salary? (Correlated sub query) SQL> select empno,ename,sal,deptno from emp E where sal>(select avg(sal) from emp where deptno=e.deptno); EMPNO ENAME SAL ---------- ---------- ---------- ---------DEPTNO

7499 ALLEN 7566 JONES 7698 BLAKE 7788 SCOTT 7839 KING 7902 FORD 6 rows selected.

1600 2975 2850 3000 5000 3000

30 20 30 20 10 20

sql operators: 1) arithmetic operators 2)comparison operators 3)logical operators 4)set operators Arithmetic operators: arithmetic operators are use to perform adding,subtraction operations on data. arithmetic operators are addition(+) subtraction(-) division(/) multiplication(*) example: update student set fee=fee+1000; update student set fee=fee-1000; update student set fee=fee/10; update student set fee=fee*10; comparison operators: <=, >=, <, >,=,NOT IN, IN,ISNULL,IS NOT NULL,LIKE,ALL,ANY (SOME), EXSITS,BETWEEN. IN / NOT IN Used to pick one by one value from list of values ,used with numbers, characters & data values Ex:select * from emp where empno in(101,102,105,107); Where empno=101 or empno=102 Or empno=105 select * from emp where empno not in(101,102,105,107); Ex:update emp set sal=sal+sal*.25 where ename in(ram,hari); Ex:delete from empwhere hiredate in(10-jan-06,10-feb-06); BETWEEN Used to pick the values with in a range. It supports with numbers & date values only. Ex: select * from emp where sal between 4000 and 7000; Update emp set sal=sal+2000,comm.=1000

Where hiredate between 1-jan-05 and 31-dec-05;

LIKE Used to search for a pattern Valid only for characters data. It uses 2 symbols 1)%-- represents zero or more characters 2)- (underscore)represents single characters. List the employees whose names start with s Select * from emp where ename like s%; List the employees whose names having 5 characters Select * from emp where ename like ____; List having 2nd characters as I Select * from emp where ename like _i%; NULL --it is an uncomparable,undefined value. --it is not equal to space or zero. --it will not occupy any memory. --it is imposed by E.f codd & every RDBMS has to support it. --it is represented with NULL keyword --when ever a column is not provided with a value while inserting it Will automatically stores a NULL value. IS NULL --used to compare NULL values. List the Employees who dont have commission Select * from emp where comm is NULL; Set operators --used to join the output of select statement. --select statements must have equal no of columns and similar data type columns. 4 types 1)Union all output of query1 + output of query2 2)Union -- output of query1 + output of query2 duplicate rows. 3)Intersect output common to query1 and query2. 4)Minus -- output of query1 output of query2(rows unique to query1). Examples

SQL> select job from emp where deptno=10 2 Union all 3 select job from emp where deptno=20; JOB --------MANAGER PRESIDENT CLERK MANAGER ANALYST CLERK ANALYST 7 rows selected. SQL> select job from emp where deptno=10 2 Union 3 select job from emp where deptno=20; JOB --------ANALYST CLERK MANAGER PRESIDENT SQL> select job from emp where deptno=10 2 Intersect 3 select job from emp where deptno=20; JOB --------MANAGER SQL> select job from emp where deptno=10 2 Minus 3 select job from emp where deptno=20; JOB --------PRESIDENT

You might also like