0% found this document useful (0 votes)
6 views26 pages

Sqlmanual 1

The document provides a comprehensive guide on creating and managing SQL tables including DEP, EMP, CUST, and SALGRADE. It includes commands for inserting data, modifying table structures, and performing various SQL queries for data retrieval and manipulation. Additionally, it covers the use of aggregate functions and subqueries for advanced data operations.

Uploaded by

ahanasayeed.786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views26 pages

Sqlmanual 1

The document provides a comprehensive guide on creating and managing SQL tables including DEP, EMP, CUST, and SALGRADE. It includes commands for inserting data, modifying table structures, and performing various SQL queries for data retrieval and manipulation. Additionally, it covers the use of aggregate functions and subqueries for advanced data operations.

Uploaded by

ahanasayeed.786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

1)Create DEP(Department) table

CREATE TABLE DEP (

DEP_ID INT PRIMARY KEY,

DNAME CHAR(10),

DMGR_ID INT

);

Table Created

COMMANDS To see the structure of a DEP table:

DESC DEP;

To Insert The values In DEP Table


SQL> INSERT INTO DEP VALUES (1, 'RESEARCH', 1003);
1 row created.

SQL> INSERT INTO DEP VALUES (2, 'ADMIN', 1002);


1 row created.

SQL> INSERT INTO DEP VALUES (3, 'PRODUCTION', 1004);


1 row created.

SQL> INSERT INTO DEP VALUES (4, 'SALES', 1005);


1 row created.

SQL> INSERT INTO DEP VALUES (5, 'HR', 1001);


1 row created.

To save the SQL COMMANDS


SQL> COMMIT;
Commit complete.
SQL Command To see the values that you inserted into a DEP table

select * from DEP;

2) Create EMP(Employee) table


CREATE TABLE EMP (
EMP_ID INT PRIMARY KEY,
FNAME CHAR(10),
LNAME CHAR(10),
JOB_ID CHAR(10),
DOJ DATE,
SAL NUMBER(10, 5),
MGR_ID INT REFERENCES EMP(EMP_ID),
DEP_ID INT REFERENCES DEP(DEP_ID)
);

Table created.

COMMAND To see the structure of a EMP table:

DESC EMP;
To Insert The values In EMP Table
SQL>INSERT INTO EMP VALUES (1001,'ANIL','CHOWDARY','MANAGER','01-JAN-2000', 25000, 1001, 2);
1 row created.

SQL> INSERT INTO EMP VALUES (1002,'SANDEEP','SHARMA','ANALYST','01-FEB-2000', 23000, 1001, 3);


1 row created.

SQL> INSERT INTO EMP VALUES (1003,'BHARATH','SINGHVI','TRAINEE','01-MAR-2000', 22000, 1001, 5);


1 row created.

SQL> INSERT INTO EMP VALUES (1004,'SUSHEEL','SHARMA','CLERK','01-APR-2000', 21000, 1001, 1);


1 row created.

SQL> INSERT INTO EMP VALUES (1005,'SACHIN','VERMA','ANALYST','01-MAY-2000', 20000, 1001, 4);


1 row created.

To save the SQL COMMANDS


SQL> COMMIT;
Commit complete.

SQL Command To see the values that you inserted into a EMP table

select * from EMP;


1)Working with Table and data using another Table
Query 1: Select employee with EMP_ID = 1003
SQL> SELECT * FROM EMP WHERE EMP_ID=1003;

Query 2: Show all the employees who are in department number 3.


SQL> SELECT * FROM EMP WHERE DEP_ID=3;

Query 3: Write an SQL query to update the salary to 18,000 for the employee whose
EMP_ID is 1004.
SQL> UPDATE EMP SET SAL = 18000 WHERE EMP_ID = 1004;

To see the updated table after making changes (like updating a salary), use the
SELECT statement.
SQL>select * from EMP;
Query 4: Write an SQL query to display the employee id (EMP_ID) and salary (SAL) of
the employee whose EMP_ID is 1004.

SQL> SELECT EMP_ID, SAL FROM EMP WHERE EMP_ID = 1004;

Query 5: Write an SQL query to display the Employee ID, First Name, Last Name, and
Department Name of employees who are department managers.

SQL> SELECT EMP_ID, FNAME, LNAME, DNAME FROM EMP, DEP WHERE EMP_ID=DMGR_ID;

Query 6:Write an SQL query to Delete the record whose EMP_ID is 1003

SQL> DELETE FROM EMP WHERE EMP_ID=1003;


3)Create CUST(Customer) table

CREATE TABLE CUST (


CUST_ID INT PRIMARY KEY,
FNAME CHAR(10),
LNAME CHAR(10),
ADDR VARCHAR2(20),
CITY CHAR(10),
PHONE INT,
EMAIL VARCHAR2(20)
);

Table created.
COMMAND To see the structure of a CUST table:

DESC CUST;

To Insert The values In CUST Table


SQL> INSERT INTO CUST VALUES (101,'ANAND','A','JAYANAGARA','DAVANAGERE',1234567890,'[email protected]');
1 row created.

SQL> INSERT INTO CUST VALUES (102,'ARAVIND','V','J P NAGAR','BENGALURU',2345678901,'[email protected]');


1 row created.

SQL> INSERT INTO CUST VALUES(103,'GANESH','G','J C EXTN','MYSORE',3456789012,'[email protected]');


1 row created.

SQL> INSERT INTO CUST VALUES (104,'SURESH','S','VIDYA NAGAR','TUMKUR', 467890123,'[email protected]');


1 row created.

SQL> INSERT INTO CUST VALUES(105,'KISHOR','K','RAJAJI NAGAR','SHIMOGA',5678901234,'[email protected]');


1 row created.
To save the SQL COMMAND
SQL> COMMIT;
Commit complete.
SQL Command To see the values that you inserted into a CUST table

select * from CUST;

4) Create SALGRADE table

CREATE TABLE SALGRADE (


GRADE CHAR(10),
HIGH_SAL NUMBER(10, 5),
LOW_SAL NUMBER(10, 5)
);

Table created.

COMMAND To see the structure of a SALGRADE table:

DESC SALGRADE;
To Insert The values In SALGRADE Table

SQL> INSERT INTO SALGRADE VALUES ('GRADE-1', 50000, 25000);


1 row created.

SQL> INSERT INTO SALGRADE VALUES ('GRADE-2', 40000, 20000);


1 row created.

SQL> INSERT INTO SALGRADE VALUES ('GRADE-3', 30000, 15000);


1 row created.

SQL> INSERT INTO SALGRADE VALUES ('GRADE-4', 25000, 12500);


1 row created.

SQL> INSERT INTO SALGRADE VALUES ('GRADE-5', 20000, 10000);


1 row created.

To save the SQL COMMAND


SQL> COMMIT;
Commit complete.

SQL Command To see the values that you inserted into a SALGRADE table

select * from SALGRADE;


2) Modifying Table Structure and updating data.

Query 1: Write an SQL Query to Rename the JOB_ID column to DESG in the EMP table

SQL> ALTER TABLE EMP RENAME COLUMN JOB_ID TO DESG;

To Check Whether the EMP table is altered or not


SQL> SELECT * FROM EMP;

Query 2: Write an SQL Query to Add a new column ADDR to the EMP table

SQL> ALTER TABLE EMP ADD ADDR CHAR(20);

To Check Whether the EMP table is altered or not


SQL> SELECT * FROM EMP;
Query 3: Write an SQL Query to Drop the ADDR column from the EMP table

SQL> ALTER TABLE EMP DROP COLUMN ADDR;

To Check Whether the EMP table is altered or not

SQL> SELECT * FROM EMP;

Query 4: Write an SQL Query to Modify the ADDR column in the CUST table to VARCHAR2(50)

SQL> ALTER TABLE CUST MODIFY ADDR VARCHAR2 (50);

Query 5: Update the HIGH_SAL in the SALGRADE table for GRADE = 'GRADE-1'

SQL> UPDATE SALGRADE SET HIGH_SAL=60000 WHERE GRADE='GRADE-1';


To see the SALGRADE table which is Updated

SQL> select * from SALGRADE;

3) Queries adding, deleting and verifying keys.

Query 1: Write an SQL query to modify the GRADE column in the SALGRADE table to CHAR(10)
and set it as the primary key.

SQL> ALTER TABLE SALGRADE MODIFY GRADE CHAR (10) PRIMARY KEY;

Query 2: Write an SQL query to drop the primary key from the SALGRADE table.

SQL> ALTER TABLE SALGRADE DROP PRIMARY KEY;

Query 3: Write an SQL query to insert a row into the SALGRADE table with values 'GRADE-3',
35000, and 15000.
SQL> INSERT INTO SALGRADE VALUES ('GRADE-4', 35000, 15000);
To check if the row was added successfully.
SQL> select * from SALGRADE;

Query 4:Write an SQL query to modify the DESG column in the EMP table to CHAR(10) and make
it NOT NULL.

SQL> ALTER TABLE EMP MODIFY DESG CHAR (10) NOT NULL;

Query 5: Write an SQL query to modify the PHONE column in the CUST table to INT and add a
UNIQUE constraint.

SQL> ALTER TABLE CUST MODIFY PHONE INT UNIQUE;


4) Using WHERE clause (Comparison, between and set comparison)

Query 1:Write an SQL query to display all details of the employee whose EMP_ID is
1005.

SQL> SELECT * FROM EMP WHERE EMP_ID=1005;

Query 2: Write an SQL query to display the first name, last name, and salary of all
employees from the EMP table.

SQL> SELECT FNAME, LNAME, SAL FROM EMP;

Query 3: Write an SQL query to display the first name, last name, and salary of the
employee whose EMP_ID is 1001.

SQL> SELECT FNAME, LNAME, SAL FROM EMP WHERE EMP_ID=1001;


Query 4: Write an SQL query to count the total number of records in the EMP table.

SQL> SELECT COUNT (*) FROM EMP;

Query 5:Write an SQL query to display all employee details where the salary is
between 20,000 and 22,000.

SQL> SELECT * FROM EMP WHERE SAL BETWEEN 20000 AND 22000;

Query 6: Write an SQL query to display all employee details where the salary is less
than or equal to 22,000.

SQL> SELECT * FROM EMP WHERE SAL <= 22000;


5) Using WHERE clause (matching characters and NULL values)

Query 1:Write an SQL query to display all employee details where the last name
contains the word 'SHARMA'.

SQL> SELECT * FROM EMP WHERE LNAME LIKE '%SHARMA%';

Query 2: Write an SQL query to display all employee details where the date of joining
(DOJ) contains the month 'JAN'.

SQL> SELECT * FROM EMP WHERE DOJ LIKE '%JAN%';

Query 3: Write an SQL query to update the last name (LNAME) to an empty value for
the employee whose EMP_ID is 1004.
SQL> UPDATE EMP SET LNAME='' WHERE EMP_ID=1004;

SQL> SELECT * FROM EMP WHERE EMP_ID=1004;


Query 4: Write an SQL query to display all employee details where the last name
(LNAME) is NULL.

SQL> SELECT * FROM EMP WHERE LNAME IS NULL;

Query 5: Write an SQL query to display all employee details where the last name
(LNAME) is not NULL.

SQL> SELECT * FROM EMP WHERE LNAME IS NOT NULL;

6) Using WHERE clause (using logical operators to Join more than one conditions).

Query 1: Write an SQL query to display all the records from the CUST table.

SQL> SELECT * FROM CUST;


Query 2: Write an SQL query to display all customer details from the CUST table
where the city is 'MYSORE' and the address contains 'J C EXTN'.

SQL> SELECT * FROM CUST WHERE CITY='MYSORE' AND ADDR LIKE '%J C EXTN';

Query 3: Write an SQL query to display the EMP_ID, FNAME, LNAME, and SAL for
employees with a salary between 22,000 and 25,000.

SQL> SELECT EMP_ID, FNAME, LNAME, SAL FROM EMP WHERE SAL > 22000 AND SAL
< 25000;

Query 4: Write an SQL query to display the EMP_ID, FNAME, LNAME, and SAL for
employees with a salary greater than or equal to 22,000 and less than or equal to
25,000.

SQL> SELECT EMP_ID, FNAME, LNAME, SAL FROM EMP WHERE SAL >= 22000 AND
SAL <= 25000;
Query 5: Write an SQL query to concatenate the FNAME and LNAME columns and
display it as NAME for all customers in the CUST table.

SQL> SELECT FNAME||''|| LNAME AS NAME FROM CUST;

Query 6: Write an SQL query to display all records from the SALGRADE table where
the grade is either 'GRADE-1' or 'GRADE-2'.

SQL> SELECT * FROM SALGRADE WHERE GRADE='GRADE-1' OR GRADE='GRADE-2';

7) Formatting the output result by putting column aliases, using expressions

Query 1: Write an SQL query to display the employee ID, first name, last name, and
increased salary (with a 25% hike) of all employees.

SQL> SELECT EMP_ID, FNAME, LNAME, SAL+SAL*0.25 AS INC_SAL FROM EMP;


Query 2: Write an SQL query to display the employee ID, first name, and last name of
all employees sorted in ascending order of first name.

SQL> SELECT EMP_ID, FNAME, LNAME FROM EMP ORDER BY FNAME;

Query 3: Write an SQL query to display the employee ID, first name, and last name of
all employees sorted in descending order of first name.

SQL> SELECT EMP_ID, FNAME, LNAME FROM EMP ORDER BY FNAME DESC;

Query 4: Write an SQL query to display the employee ID, first name, last name, and
department name of all employees by joining the EMP and DEP tables. Sort the
result by employee ID.

SQL> SELECT EMP_ID, FNAME, LNAME, DNAME FROM EMP E, DEP D WHERE E.DEP_ID=D.DEP_ID ORDER BY EMP_ID;
8) Using subqueries in WHERE clause (Set membership, Set comparison, test for
empty relations).

Query 1: Write an SQL query to retrieve all employee details for employees whose
EMP_ID is either 1001, 1002, or 1003.

SQL> SELECT * FROM EMP WHERE EMP_ID IN (1001, 1002, 1003);

Query 2: Write an SQL query to retrieve all employee details for employees whose
EMP_ID is not 1001, 1002, or 1003.

SQL> SELECT * FROM EMP WHERE EMP_ID NOT IN (1001, 1002, 1003);

Query 3: Write an SQL query to display EMP_ID, first name, last name, and
department ID of all employees who are not in department 3.

SQL> SELECT EMP_ID, FNAME, LNAME, DEP_ID FROM EMP WHERE EMP_ID NOT IN (SELECT EMP_ID FROM EMP WHERE
DEP_ID=3);
Query 4: Write an SQL query to update the department manager ID to 1001 for
departments with department IDs 1 and 3.

UPDATE DEP SET DMGR_ID=1001 WHERE DEP_ID IN (1,3);

Query 5: Write an SQL query to display employee ID, first name, and last name of
employees who are managers of any department.

SQL> SELECT EMP_ID, FNAME, LNAME FROM EMP WHERE EMP_ID IN (SELECT DMGR_ID FROM DEP);

Query 6: Write an SQL query to display employee ID, first name, and last name of
employees who are not department managers.

SQL> SELECT EMP_ID, FNAME, LNAME FROM EMP WHERE EMP_ID NOT IN (SELECT
DMGR_ID FROM DEP);
Query 7: Write an SQL query to find the minimum salary among all employees.
SQL> SELECT MIN(SAL) FROM EMP;

Query 8: Write an SQL query to find the second lowest salary among all employees
using a subquery.

SQL> SELECT MIN(SAL) AS SEC_MIN_SAL FROM EMP WHERE SAL > (SELECT MIN(SAL)
FROM EMP);

9) Subqueries in FROM clause.

Query1: Write an SQL query to display the first name and salary of employees whose
salary is between 22,000 and 25,000 using a subquery.
SQL> SELECT FNAME, SAL FROM (SELECT * FROM EMP WHERE SAL BETWEEN 22000 AND 25000);

Query2: Write an SQL query to retrieve the Gmail email addresses of all customers
using a subquery.
SQL> SELECT EMAIL FROM (SELECT EMAIL FROM CUST WHERE EMAIL LIKE '%gmail%');
Query3: Write an SQL query to display the first name, last name, and date of joining
of employees who joined between 01-JAN-2000 and 01-MAR-2000 using a subquery.

SQL> SELECT FNAME, LNAME, DOJ FROM (SELECT * FROM EMP WHERE DOJ
BETWEEN '01-JAN-2000' AND '01-MAR-2000');

Query4: Write an SQL query to update the phone number to blank for the customer
whose customer ID is 103.

SQL> UPDATE CUST SET PHONE='' WHERE CUST_ID=103;

Query5: Write an SQL query using a subquery to retrieve all details of customers
whose phone number is null.

SQL> SELECT * FROM (SELECT * FROM CUST WHERE PHONE IS NULL);


10) Aggregate functions.

Query1: Write an SQL query to display the department ID and the number of
employees in each department.

Query2: Write an SQL query to update the department ID to 3 for employees with
IDs 1001 and 1004.

SQL> UPDATE EMP SET DEP_ID=3 WHERE EMP_ID IN (1001,1004);

Query3: Write an SQL query to update the department ID to 4 for employees with
IDs 1002 and 1005.

SQL> UPDATE EMP SET DEP_ID=4 WHERE EMP_ID IN (1002,1005);

Query4: Write an SQL query to display all records from the EMP table.

SQL> SELECT * FROM EMP;


Query5: Write an SQL query to display the department ID, number of employees, and
average salary for departments having at least 2 employees.

SQL> SELECT DEP_ID, COUNT (*), AVG (SAL) FROM EMP GROUP BY DEP_ID HAVING COUNT(*) >= 2;

Query6: Write an SQL query to display the department ID, number of employees,
and average salary for each department.
SQL> SELECT DEP_ID, COUNT (*), AVG (SAL) FROM EMP GROUP BY DEP_ID;

Query 7: Write an SQL query to display the department ID, number of employees,
minimum salary, and maximum salary for each department.

SQL> SELECT DEP_ID, COUNT(*), MIN(SAL), MAX(SAL) FROM EMP GROUP BY DEP_ID;

Query 8: Write an SQL query to calculate the total salary of all employees.

SQL> SELECT SUM(SAL) FROM EMP;

You might also like