You are on page 1of 23

INDEX

S.No
1 2 3 4 5 6 7 8 9 10 11 12 13

TOPIC
Introduction to SQL To study basic SQL commands To study viewing commands (select, update) To study commands to modify structure (alter delete) To study condition commands (and, or, in, not, like) To study aggregate functions To study grouping commands To study commands involving data constraints To study the commands for aliasing and renaming To study the commands for join To study the various set operations To study scalar and string functions To study commands for views

DATE

SIGN

EXPERIMENT NO-01

AIM-

Introduction to SQL

DEFINATION
SQL stands for Structured Query Language.

It is non procedural in nature. SQL standard specifies data definition, data manipulation and other associated facilities of DBMS that supports the relational data Model. It is comprehensive language for controlling and interacting with the DBMS.

FEATURES OF SQL
SQL is a high level language. SQL is a free format syntax which gives users the ability to

structure SQL statements on any part of the screen. SQL is very flexible. SQL based applications are portable i.e. they can be run on any system.

SQL UTILITIES SQL can execute queries against a database SQL can retrieve data from a database

SQL can insert records in a database SQL can update records in a database SQL can delete records from a database SQL can create new databases SQL can create new tables in a database SQL can create stored procedures in a database SQL can create views in a database SQL can set permissions on tables, procedures, and views. SQL can be divided into two parts: Data Manipulation Language (DML) - Data Definition Language (DDL).
-

DATA MANIPULATION LANGUAGE (DML) DML is used to change the data within the data.The query and update commands form the DML part of SQL: SELECT - extracts data from a database UPDATE - updates data in a database DELETE - deletes data from a database INSERT - inserts new data into a database

DATA DEFINATION LANGUAGE (DDL) The DDL part of SQL permits to create and remove database objects. The most important DDL statements in SQL are: CREATE - creates a new object like table, index and view. ALTER to modify the structure of the table, constraints. DROP - to drop the entire object.

Difference between SQL and PL/SQL

SQL It is data oriented language for selecting & manipulating data. It executes one statement at a time. It is used to write queries, DDL and DML. It is declarative.

PL/SQL It is a procedural language to create applications. Here block of code can be executed. It is used to write program blocks, Triggers, Functions, Procedures and Packages. It is procedural.

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

EXPERIMENT NO-02

AIMTo study Basic SQL commands (create database, create table, use, drop, insert) and execute the following queries using these commands: 1 Create a database named ' Employee'. 2 Use the database 'Employee' and create a table 'Emp' with attributes 'ename', 'ecity', 'salary', 'enumber', 'eaddress', 'depttname'. 3 Create another table 'Company' with attributes 'cname',' ccity','numberof emp','empnumber' in the database 'Employee'..

SQL CODESQL> Create table emp(ename varchar(20), ecity varchar(20),salary integer ,enumber integer, eaddress varchar(20), depttname varchar(20) ); Table created. SQL> Create table company(cname varchar(20), ccity varchar(20), numberofemp integer ,empnumber integer ); Table created. SQL> insert into emp values('sahil' , 'delhi', 45000 , 120 , 'a-5 r c delhi' , 'ECE'); 1 row created. SQL> insert into emp values('prakash' , 'mumbai', 55000 ,111 , 'b-6 juhu' , 'ECE'); 1 row created. SQL> insert into emp values('prachi' , 'delhi', 45000 , 121 , '36 rn delhi' , 'ECE'); 1 row created. SQL> insert into emp values('gagan' , 'gurgaon', 15000 , 118 , '29 gagan vihar' , 'HR');

1 row created. SQL> insert into emp values('nisha' , 'delhi', 18000, 101 , 'd 2 dwarka ' , 'HR'); 1 row created. SQL> insert into emp values('sourav' , 'mumbai', 10000 ,114 ,'b9 juhu,' IT'); 1 row created. SQL> insert into emp values('anita' , 'delhi',28000 , 104, '23 nsp' , 'CSE'); 1 row created. SQL> select * from emp;

ENAME ECITY SALARY ENUMBER EADDRESS DEPTTNAME -------------------- -------------------- ----------------------- ----------------------------------sahil delhi 45000 120 a-5 r c delhi ECE prakash mumbai 55000 111 b-6 juhu ECE prachi delhi 45000 121 36 rn delhi ECE gagan gurgaon 15000 118 29 gagan vihar HR nisha delhi 18000 101 d 2 dwarka HR sourav mumbai 10000 114 b9 juhu IT anita delhi 28000 104 23 nsp CSE

7 rows selected. SQL> insert into company values('TCS' , 'delhi',465456 ,120 ); 1 row created. SQL> insert into company values('TCS' , 'delhi', 465456,121 ); 1 row created. SQL> insert into company values('KJP' , 'mumbai',56545 ,111 ); 1 row created. SQL> insert into company values('TCS' , 'gurgaon', 465456 ,118 ); 1 row created. SQL> insert into company values('TCS' , 'delhi', 465456 ,101 ); 1 row created.

SQL> insert into company values('TCS' , 'delhi', 465456 ,104 ); 1 row created. SQL> insert into company values('itm' , 'mumbai', 18456 ,114 ); 1 row created. SQL> select * from company;

CNAME CCITY NUMBEROFEMP EMPNUMBER -------------------- -------------------- ---------------------------------------TCS delhi 465456 120 TCS delhi 465456 121 KJP mumbai 56545 111 TCS gurgaon 465456 118 TCS delhi 465456 101 TCS delhi 465456 104 itm mumbai 18456 114

7 rows selected.

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

EXPERIMENT NO-03 AIMTo study the viewing commands (select , update) and execute the following queries using these commands: 1 Find the names of all employees who live in Delhi. 2 Increase the salary of all employees by Rs. 5,000. 3 Find the company names where the number of employees is greater than 10,000. 4 Change the Company City to Gurgaon where the Company name is 'TCS'.

SQL CODESQL> Select ename from emp where ecity = 'delhi'; ENAME -------------------sahil prachi nisha anita

SQL> Update emp set salary=salary+5000; 7 rows updated. SQL> SELECT SALARY FROM EMP;

SALARY ---------50000 60000 50000 20000 23000 15000 33000 7 rows selected. SQL> Select cname from company where numberofemp > 10000; CNAME -------------------TCS TCS KJP TCS TCS TCS itm 7 rows selected. SQL> Update company set ccity = 'Gurgaon' where cname = 'TCS'; 5 rows updated. SQL> select cname,ccity from company; CNAME CCITY -------------------- -------------------TCS Gurgaon TCS Gurgaon KJP mumbai TCS Gurgaon TCS Gurgaon TCS Gurgaon itm mumbai 7 rows selected.

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

EXPERIMENT NO-04 AIMTo study the commands to modify the structure of table (alter, delete) and execute the following queries using these commands: 1 Add an attribute named ' Designation' to the table 'Emp'. 2 Modify the table 'Emp', Change the datatype of 'salary' attribute to float. 3 Drop the attribute 'depttname' from the table 'emp'. 4 Delete the entries from the table ' Company' where the number of employees are less than 500.

SQL CODE

SQL> ALTER TABLE emp ADD designation varchar(20); Table altered. SQL> ALTER TABLE emp MODIFY salary float; Table altered. SQL> ALTER TABLE emp DROP COLUMN depttname; Table altered. SQL> DELETE FROM Company WHERE numberofemp < 500; 0 rows deleted. SQL> select * from emp;

ENAME ECITY SALARY ENUMBER EADDRESS Designation -------------------- -------------------- ----------------------- ----------------------------------Sahil delhi 45000 120 a-5 r c delhi Prakash mumbai 55000 111 b-6 juhu Prachi delhi 45000 121 36 rn delhi Gagan gurgaon 15000 118 29 gagan vihar Nisha delhi 18000 101 d 2 dwarka sourav mumbai 10000 114 b-9 juhu anita delhi 28000 104 23 nsp

7rows selected.

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

EXPERIMENT NO-05 AIMTo study the commands that involve compound conditions (and, or, in , not in, between , not between , like , not like) and execute the following queries using these commands: 1 Find the names of all employees who live in ' Gurgaon' and whose salary is between Rs. 20,000 and Rs. 30,000. 2 Find the names of all employees whose names begin with either letter 'A' or 'B'. 3 Find the company names where the company city is 'Delhi' and the number of employees is not between 5000 and 10,000. 4 Find the names of all companies that do not end with letter 'A'.

SQL CODE
SQL> Select ename from emp where ecity = 'Gurgaon' AND salary between 20000 and 30000; no rows selected SQL> Select ename from emp where ename like 'a%' OR ename LIKE 'b %'; ENAME -------------------anita SQL> select cname from company where ccity = 'delhi' AND NOT (numberofemp between 5000 and 10000); no rows selected

SQL> select cname from company where cname not like '% A'; CNAME -------------------TCS TCS KJP TCS TCS TCS itm 7 rows selected.

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

EXPERIMENT NO-06 AIMTo study the aggregate functions (sum, count, max, min, average) and execute the following queries using these commands: 1 Find the sum and average of salaries of all employees in computer science department. 2 Find the number of all employees in company 'TCS'. 3 Find the maximum and the minimum salary in the HR department.
SQL CODE SQL> Select sum(salary) , avg(salary) from emp where depttname = 'CSE'; SUM(SALARY) AVG(SALARY) ----------- ----------33000 33000 SQL> select count(numberofemp) from company where cname = 'TCS'; COUNT(NUMBEROFEMP) ------------------5 SQL> select max(salary) , min(salary) from emp where depttname ='HR'; MAX(SALARY) MIN(SALARY) ----------- ----------23000 20000

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

EXPERIMENT NO-07 AIMTo study the grouping commands (group by, order by) and execute the following queries using these commands: 1 List all employee names in descending order. 2 Find number of employees in each department where number of employees is greater than 5. 3 List all the department names where average salary of a department is Rs.10,000.

SQL CODE
SQL> Select ename from emp order by ename desc;
ENAME -------------------sourav sahil prakash prachi nisha gagan anita

7 rows selected. SQL> Select count(ename) from emp depttname where enumber > 5;
COUNT(ENAME) -----------7

SQL> Select depttname from emp group by depttname having avg(salary) = 10000; no rows selected **********************

EXPERIMENT NO-08 AIMTo study the commands involving data constraints and execute the following queries using these commands: 1 Alter table 'Emp' and make 'enumber' as the primary key. 2 Alter table 'Company4' and add the foreign key constraint. 3 Add a check constraint in the table 'Emp' such that salary has the value between 0 and Rs.1,00,000.

SQL CODE
SQL> ALTER TABLE emp ADD PRIMARY KEY (enumber); Table altered. SQL> ALTER TABLE company ADD FOREIGN KEY (empnumber) REFERENCES emp(enumber); Table altered. SQL> ALTER TABLE emp ADD CONSTRAINT checksal CHECK(salary < 100000); Table altered.

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

EXPERIMENT NO-09 AIMTo study the commands for aliasing and renaming and execute the following queries using these commands: 1 Rename the name of database to 'Employee1'. 2 Rename the name of table 'Emp' to 'Emp1'. 3 Change the name of the attribute 'ename' to 'empname'

SQL CODE
SQL> rename employee to employee1; Table renamed. SQL> rename emp to emp1; Table renamed. SQL> alter table emp1 add (emplyname varchar(20) ); Table altered. SQL> update emp1 set emplyname = ename; 7 rows updated. SQL> alter table emp1 drop column ename; Table altered. SQL> select * from emp1;
ENAME ECITY SALARY ENUMBER EADDRESS DEPTTNAME -------------------- -------------------- ----------------------- ----------------------------------sahil delhi 45000 120 a-5 r c delhi ECE prakash mumbai 55000 111 b-6 juhu ECE prachi delhi 45000 121 36 rn delhi ECE gagan gurgaon 15000 118 29 gagan vihar HR nisha delhi 18000 101 d 2 dwarka HR sourav mumbai 10000 114 b-9 juhu IT anita delhi 28000 104 23 nsp CSE

7 rows selected. **********************

EXPERIMENT NO-10 AIMTo study the commands for joins ( cross join, inner join, outer join) and execute the following queries using these commands: 1 Retrieve the complete record of an employee and its company from both the table using joins. 2 List all the employees working in the company 'TCS'.

SQL CODE
SQL> Select * from emp Join Company on emp.enumber = company.empnumber;
ECITY SALARY ENUMBER EADDRESS DEPTTNAME EMPLYNAME CNAME CCITY NUMBEROFEMP EMPNUMBER -------------------- ---------- ---------- ---------------------------------------- -------------------- --------------------------------- ------------ ---------delhi 23000 101 d 2 dwarka HR nisha TCS Gurgaon 465456 101 delhi 33000 104 111 114 118 120 121 23 nsp b-6 juhu b-9 juhu 29 gagan vihar a-5 r c delhi 36 m delhi CSE EEE IT HR ECE ECE anita prakash saurav gagan sahil prachi TCS KJP itm TCS TCS TCS Gurgaon calcutta mumbai Gurgaon Gurgaon Gurgaon 465456 56545 18456 465456 465456 465456 104 111 114 118 120 121

mumbai 60000 mumbai 15000 gurgaon 20000 delhi delhi 50000 50000

7 rows selected SQL> Select * from emp Join Company on emp.enumber = company.empnumber where company.cname = 'TCS';
ECITY SALARY ENUMBER EADDRESS DEPTTNAME EMPLYNAME CNAME CCITY NUMBEROFEMP EMPNUMBER

delhi delhi

-------------------- ---------- ---------- --------------------------------------- -------------------- ---------------------------------- -----23000 101 d 2 dwarka HR nisha TCS Gurgaon 465456 101 33000 104 118 120 121 23 nsp 29 gagan vihar a-5 r c delhi 36 m delhi CSE HR ECE ECE ania gagan sahil prachi TCS Gurgaon TCS Gurgaon TCS TCS Gurgaon Gurgaon 465456 465456 465456 465456 104 118 120 121

gurgaon 20000 delhi delhi 50000 50000

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

EXPERIMENT NO-11 AIMTo study the various set operations and execute the following queries using these commands: 1. List the enumber of all employees who live in Delhi and whose comp is in Gurgaon or if both conditions are true. 2. List the enumber of all employees who live in Delhi but whose company is not in Gurgaon.

SQL CODE
SQL> SELECT enumber FROM Emp WHERE ecity='Delhi' UNION SELECT empnumber FROM Company WHERE ccity='Gurgaon'; ENUMBER ---------101 104 118 120 121 SQL> SELECT enumber FROM Emp WHERE ecity='Delhi' UNION SELECT empnumber FROM Company WHERE ccity != 'Gurgaon'; ENUMBER ---------111 114 **********************

EXPERIMENT NO-12 AIMTo study the various scalar functions and string functions ( power, square, substring, reverse, upper, lower, concatenation) and execute the following queries using these commands: 1 Reverse the names of all employees. 2 Change the names of company4 cities to uppercase. 3 Concatenate name and city of the employee.

SQL CODE
SQL> SELECT REVERSE(ename) FROM Emp; REVERSE(ENAME) -------------------lihas hsakarp ihcarp nagag ahsin varuos atina 7 rows selected. SQL> SELECT UPPER(ccity) FROM Company; UPPER(CCITY) -------------------GURGAON GURGAON MUMBAI GURGAON GURGAON GURGAON MUMBAI

7 rows selected. SQL> SELECT CONCAT(ename, ecity) FROM Emp; CONCAT(ENAME,ECITY) ---------------------------------------sahildelhi prakashmumbai prachidelhi gagangurgaon nishadelhi souravmumbai anitadelhi 7 rows selected.

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

EXPERIMENT NO-13 AIMTo study the commands for views and execute the following queries using these commands: 1 Create a view having ename and ecity. 2 In the above view change the ecity to 'Delhi' where ename is 'John'. 3 Create a view having attributes from both the tables. 4 Update the above view and increase the salary of all employees of IT department by Rs.1000.

SQL CODE
SQL> CREATE VIEW V AS SELECT ENAME, ECITY FROM EMP; View created. SQL> UPDATE V SET ECITY = 'DELHI' WHERE ENAME = 'JOHN'; 0 rows updated. SQL> CREATE VIEW S AS SELECT ENUMBER FROM EMP,COMPANY; View created. SQL>UPDATE S SET SALARY = SALARY + 1000 WHERE DEPTNAME = 'IT'; view created.

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

You might also like