Sqlmanual 1
Sqlmanual 1
DNAME CHAR(10),
DMGR_ID INT
);
Table Created
DESC DEP;
Table created.
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 Command To see the values that you inserted into a EMP table
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.
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
Table created.
COMMAND To see the structure of a CUST table:
DESC CUST;
Table created.
DESC SALGRADE;
To Insert The values In SALGRADE Table
SQL Command To see the values that you inserted into a SALGRADE table
Query 1: Write an SQL Query to Rename the JOB_ID column to DESG in the EMP table
Query 2: Write an SQL Query to Add a new column ADDR to the EMP table
Query 4: Write an SQL Query to Modify the ADDR column in the CUST table to VARCHAR2(50)
Query 5: Update the HIGH_SAL in the SALGRADE table for GRADE = 'GRADE-1'
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.
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.
Query 1:Write an SQL query to display all details of the employee whose EMP_ID is
1005.
Query 2: Write an SQL query to display the first name, last name, and salary of all
employees from the EMP table.
Query 3: Write an SQL query to display the first name, last name, and salary of the
employee whose EMP_ID is 1001.
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.
Query 1:Write an SQL query to display all employee details where the last name
contains the word 'SHARMA'.
Query 2: Write an SQL query to display all employee details where the date of joining
(DOJ) contains the month '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;
Query 5: Write an SQL query to display all employee details where the last name
(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 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.
Query 6: Write an SQL query to display all records from the SALGRADE table where
the grade is either 'GRADE-1' or 'GRADE-2'.
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.
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.
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.
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);
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.
Query5: Write an SQL query using a subquery to retrieve all details of customers
whose phone number is null.
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.
Query3: Write an SQL query to update the department ID to 4 for employees with
IDs 1002 and 1005.
Query4: Write an SQL query to display all records from the EMP table.
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.