0% found this document useful (0 votes)
38 views

SQL Query 18 APR 2023

The document contains examples of SQL queries on an EMP database table. Some key examples include: 1) Finding employees with 'a' as the second letter in their name and those who joined in June. 2) Displaying names in alphabetical order and calculating average salary. 3) Finding employees with the same salary as 'Tamal' and converting join dates to DD/MM/YY format. 4) Checking for departments with more than 3 employees and calculating highest-lowest salary difference. 5) Displaying lowest paid employee and structure of EMP table, sorting by salary. 6) Checking for employees hired in 2002.

Uploaded by

shreya mitra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

SQL Query 18 APR 2023

The document contains examples of SQL queries on an EMP database table. Some key examples include: 1) Finding employees with 'a' as the second letter in their name and those who joined in June. 2) Displaying names in alphabetical order and calculating average salary. 3) Finding employees with the same salary as 'Tamal' and converting join dates to DD/MM/YY format. 4) Checking for departments with more than 3 employees and calculating highest-lowest salary difference. 5) Displaying lowest paid employee and structure of EMP table, sorting by salary. 6) Checking for employees hired in 2002.

Uploaded by

shreya mitra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1. a) Display all employees having "a" as the second letter in their names.

Select * from EMP where Ename LIKE '_a%';

ENO ENAME CITY SALARY DNO JOIN_DATE


E2 Kamal Mumbai 18000 2 02-JAN-02
E3 Tamal Chennai 7000 1 07-FEB-04

b) Display employee names for those who joined in the month of Jun.

Select Ename from EMP where substr(to_char(Join_date),4,3)='Jun';


Ename
Ashim
Timir
c) Display names of all employees in the alphabetic order. d) Find the average salary of all employees.

Select * from EMP order by Ename ASC;


ENO ENAME CITY SALARY DNO JOIN_DATE
E4 Asha Kolkata 8000 2 01-MAR-07
E1 Ashim Kolkata 10000 1 01-JUN-02
E2 Kamal Mumbai 18000 2 02-JAN-02
E3 Tamal Chennai 7000 1 07-FEB-04
E5 Timir Delhi 7000 1 11-JUN-05

d) Find the average salary of all employees

Select avg(Salary) as avg_salary from EMP;


AVG_SALARY
10000
2. a) Display employee’s names and department names of all employees who belong to either
"Chennai", or "Kolkata” or "Mumbai".
Select Ename,Dname from EMP,DEPT where EMP.Dno=DEPT.Dno AND
City='Chennai' OR City='Kolkata' OR City='Mumbai';
ENAME DNAME
Kamal Research
Kamal Finance
Kamal Project
Kamal Administration
Kamal System
Tamal Research
Ashim Research
Ashim Finance
Ashim Project
Ashim Administration
Ashim System
Asha Research
Asha Finance
Asha Project
Asha Administration
Asha System

b) List all the employee names whose basic is greater than 7000 and less than 18000.
Select Ename,Salary from EMP where Salary>7000 and Salary<18000;
ENAME SALARY
Ashim 10000
Asha 8000

c) Display list of all employees in department no. 2.


Select Ename,Dname from EMP,DEPT where EMP.Dno=DEPT.Dno AND Dname='Finance';
ENAME DNAME
Kamal Finance
Asha Finance
d) Display the no. of employees in each department.
Select Dno,count(*)AS Total_Employee from EMP group by Dno;
DNO TOTAL_EMPLOYEE
1 3
2 2

3. a) List only the names of all other employees who get the same basic pay as that of employee
"Tamal".

select Ename from EMP where Salary=(Select Salary from EMP where
Ename='Tamal');
ENAME
Tamal
Timir

b) Display the joining date of all employees in “dd/mm/yy” format.

Select to_char(Join_date,'DD/MM/YY')AS Join_date_formatted from


EMP;
JOIN_DATE_FORMATTED
02/01/02
07/02/04
01/06/02
01/03/07
11/06/05

c) Find all departments that have more than 3 employees.

Select DEPT.Dname from EMP,DEPT where EMP.Dno=DEPT.Dno group by


DEPT.Dname having count(*)>3;
no data found

d) Find the difference between highest and lowest salary.

select MAX(Salary)-MIN(Salary)AS Difference from EMP;


DIFFERENCE
11000
4. a) Display the names of all employees who are engaged in two or more projects.

Select Ename,count(Pno) AS NoProj from PROJECT1,EMP where


PROJECT1.Eno=EMP.Eno group by EMP.Eno having NoProj>=2;

no data found

b) List of all employees who have salary between 2000 &10000.

select * from EMP where Salary BETWEEN 2000 AND 10000;


ENO ENAME CITY SALARY DNO JOIN_DATE
E3 Tamal Chennai 7000 1 07-FEB-04
E1 Ashim Kolkata 10000 1 01-JUN-02
E4 Asha Kolkata 8000 2 01-MAR-07
E5 Timir Delhi 7000 1 11-JUN-05

c) List details of all employees in department number 2 & 1

Select * from EMP where Dno=1 OR Dno=2;


ENO ENAME CITY SALARY DNO JOIN_DATE
E2 Kamal Mumbai 18000 2 02-JAN-02
E3 Tamal Chennai 7000 1 07-FEB-04
E1 Ashim Kolkata 10000 1 01-JUN-02
E4 Asha Kolkata 8000 2 01-MAR-07
E5 Timir Delhi 7000 1 11-JUN-05

5. a) Display employee number, employee name and basic pay for employees with lowest salary.

Select Eno,Ename,Salary from EMP where Salary=(select


min(Salary) from EMP);
b) Display the structure of table EMP.

DESC EMP;
Column Null? Type
ENO NOT NULL CHAR(2)
ENAME - VARCHAR2(10)
CITY - VARCHAR2(10)
SALARY - NUMBER
DNO - NUMBER
JOIN_DATE - DATE

c) List the name and the salary of all employee sorted' by salary.

Select * from EMP order by Salary ASC;


ENO ENAME CITY SALARY DNO JOIN_DATE
E3 Tamal Chennai 7000 1 07-FEB-04
E5 Timir Delhi 7000 1 11-JUN-05
E4 Asha Kolkata 8000 2 01-MAR-07
E1 Ashim Kolkata 10000 1 01-JUN-02
E2 Kamal Mumbai 18000 2 02-JAN-02

d) Display the list of all employees who were hired during 2002

Select Ename,Join_date from EMP where Join_date like '%02';


ENAME JOIN_DATE
Kamal 02-JAN-02
Ashim 01-JUN-02

6. a) Display employee name and basic pay for all employees who are engaged with at least one
project.

b) List of all employees who have name exactly 4 characters.

Select Ename from EMP where Ename like '____';


ENAME
Asha
c) List no. of projects undertaken in the department 1.

You might also like