SQL Queries and Database Basics
Here we are going to understand database basics, starting with creating simple tables.
Create the following tables
Queries Based on tables
Simple Queries:
Prob: List all the employee details.
Sol : SQL > Select * from employee;
Prob: List all the department details
Sol: SQL > Select * from department;
Prob: List all job details
Sol: SQL > Select * from job;
Prob: List all the locations
Sol: SQL > Select * from loc;
Prob: List out first name,last name,salary, commission for all employees
Sol: SQL > Select first_name, last_name, salary, commission from employee;
Prob: List out employee_id,last name,department id for all employees and rename
employee id as “ID of the employee”, last name as “Name of the employee”,
department id as “department ID”
Sol: SQL > Select employee_id “id of the employee”, last_name “name", department
id as “department id” from employee;
Prob: List out the employees anuual salary with their names only.
Sol: SQL > Select last_name, salary*12 “annual salary” from employee
Where Conditions:
Prob: List the details about “SMITH”
Sol: SQL > Select * from employee where last_name=’SMITH’;
Prob: List out the employees who are working in department 20
Sol: SQL > Select * from employee where department_id=20
Prob: List out the employees who are earning salary between 3000 and 4500
Sol: SQL > Select * from employee where salary between 3000 and 4500
Prob: List out the employees who are working in department 10 or 20
Sol: SQL > Select * from employee where department_id in (20,30)
Prob: Find out the employees who are not working in department 10 or 30
Sol: SQL > Select last_name, salary, commission, department_id from employee where
department_id not in (10,30)
Prob: List out the employees whose name starts with “S”
Sol: SQL > Select * from employee where last_name like ‘S%’
Prob:List out the employees whose name start with “S” and end with “H”
Sol: SQL > Select * from employee where last_name like ‘S%H’
Prob: List out the employees whose name length is 4 and start with “S”
Sol: SQL > Select * from employee where last_name like ‘S___’
Prob: List out the employees who are working in department 10 and draw the salaries more than 3500
Sol:SQL > Select * from employee where department_id=10 and salary>3500
Prob:list out the employees who are not receiving commission.
Sol: SQL > Select * from employee where commission is Null
Order By Clause:
Prob:List out the employee id, last name in ascending order based on the employee id.
Sol: SQL > Select employee_id, last_name from employee order by employee_id
Prob:List out the employee id, name in descending order based on salary column
Sol: SQL > Select employee_id, last_name, salary from employee order by salary desc
Prob:list out the employee details according to their last_name in ascending order and
salaries in descending order
Sol: SQL > Select employee_id, last_name, salary from employee order by last_name,
salary desc
Prob:list out the employee details according to their last_name in ascending order and
then on department_id in descending order.
Sol: SQL > Select employee_id, last_name, salary from employee order by last_name,
department_id desc
Group By & Having Clause:
Prob: How many employees who are working in different departments wise in the
organization
Sol: SQL > Select department_id, count(*), from employee group by department_id
Prob: List out the department wise maximum salary, minimum salary, average salary of
the employees
Sol: SQL > Select department_id, count(*), max(salary), min(salary), avg(salary) from
employee group by department_id
Prob: List out the job wise maximum salary, minimum salary, average salaries of the
employees.
Sol: SQL > Select job_id, count(*), max(salary), min(salary), avg(salary) from
employee group by job_id
Prob: List out the no.of employees joined in every month in ascending order.
Sol: SQL > Select to_char(hire_date,’month’)month, count(*) from employee group by
to_char(hire_date,’month’) order by month
Prob: List out the no.of employees for each month and year, in the ascending order
based on the year, month.
Sol: SQL > Select to_char(hire_date,’yyyy’) Year, to_char(hire_date,’mon’) Month,
count(*) “No. of employees” from employee group by to_char(hire_date,’yyyy’),
o_char(hire_date,’mon’)
Prob: List out the department id having atleast four employees.
Sol: SQL > Select department_id, count(*) from employee group by department_id having count(*)>=4
Prob: How many employees in January month.
Sol : SQL > Select to_char(hire_date,’mon’) month, count(*) from employee group by
to_char(hire_date,’mon’) having to_char(hire_date,’mon’)=’jan’
Prob: How many employees who are joined in January or September month.
Sol: SQL > Select to_char(hire_date,’mon’) month, count(*) from employee group by
to_char(hire_date,’mon’) having to_char(hire_date,’mon’) in (‘jan’,’sep’)
Prob: How many employees who are joined in 1985.
Sol : SQL > Select to_char(hire_date,’yyyy’) Year, count(*) from employee group by
to_char(hire_date,’yyyy’) having to_char(hire_date,’yyyy’)=1985
Prob: How many employees joined each month in 1985.
Sol: SQL > Select to_char(hire_date,’yyyy’)Year, to_char(hire_date,’mon’) Month, count(*) “No. of employees” from employee where to_char(hire_date,’yyyy’)=1985 group by to_char(hire_date,’yyyy’),to_char(hire_date,’mon’)
Prob: How many employees who are joined in March 1985.
Sol: SQL > Select to_char(hire_date,’yyyy’)Year, to_char(hire_date,’mon’) Month,
count(*) “No. of employees” from employee where to_char(hire_date,’yyyy’)=1985 and
to_char(hire_date,’mon’)=’mar’ group by to_char(hire_date,’yyyy’),
to_char(hire_date,’mon’)
Prob: Which is the department id, having greater than or equal to 3 employees joined in
April 1985.
Sol: SQL > Select department_id, count(*) “No. of employees” from employee where
to_char(hire_date,’yyyy’)=1985 and to_char(hire_date,’mon’)=’apr’ group by
to_char(hire_date,’yyyy’), to_char(hire_date,’mon’), department_id having
count(*)>=3
Sub-Queries
Prob: Display the employee who got the maximum salary.
Sol: SQL > Select * from employee where salary=(select max(salary) from employee)
Prob: Display the employees who are working in Sales department
Sol : SQL > Select * from employee where department_id IN (select department_id from
department where name=’SALES’)
Prob:Display the employees who are working as “Clerk”.
Sol: SQL > Select * from employee where job_id in (select job_id from job where
function=’CLERK’
Prob: Display the employees who are working in “New York”
Sol: SQL > Select * from employee where department_id=(select department_id from
department where location_id=(select location_id from location where regional_group=
’New York’))
Prob: Find out no.of employees working in “Sales” department.
Sol: SQL > Select * from employee where department_id=(select department_id from
department where name=’SALES’ group by department_id)
Prob: Update the employees salaries, who are working as Clerk on the basis of 10%.
Sol: SQL > Update employee set salary=salary*10/100 wehre job_id=(select job_id from job where function=’CLERK’)
Prob: Delete the employees who are working in accounting department.
Sol : SQL > delete from employee where department_id=(select department_id from department where name=’ACCOUNTING’)
Prob: Display the second highest salary drawing employee details.
Sol: SQL > Select * from employee where salary=(select max(salary) from employee where salary <(select max(salary) from employee))
Prob: Display the Nth highest salary drawing employee details.
Sol: SQL > Select distinct e.salary from employee where & no-1=(select count(distinct salary) from employee where sal>e.salary)
Sub-Query operators: (ALL,ANY,SOME,EXISTS)
Prob: List out the employees who earn more than every employee in department 30.
Sol: SQL > Select * from employee where salary > all (Select salary from employee where department_id=30)
Prob: List out the employees who earn more than the lowest salary in department 30.
Sol: SQL > Select * from employee where salary > any (Select salary from employee where department_id=30)
Prob: Find out whose department has not employees.
Sol: SQL > Select employee_id, last_name, department_id from employee e where not exists (select department_id from department d where d.department_id=e.department_id)
Prob: Find out which department does not have any employees.
Sol: SQL > Select name from department d where not exists (select last_name from employee e where d.department_id=e.department_id)
Co-Related Sub Queries:
Prob: find out the employee who can earn greater than the average salary for their department.
Sol: SQL > Select employee_id, last_name, salary, department_id from employee e where salary > (select avg(salary) from employee where department_id=e.department_id)
Joins
Simple join
Prob: List our employees with their department names
Sol: SQL > Select employee_id, last_name, name from employee e, department d where e.department_id=d.department_id
Prob: Display employees with their designations (jobs)
Sol: SQL > Select employee_id, last_name, function from employee e, job j where
e.job_id=j.job_id
Prob: Display the employees with their department name and regional groups
Sol: SQL > Select employee_id, last_name, name, regional_group from employee e, department d, location l where e.department_id=d.department_id and d.location_id=l.location_id
Prob: How many employees who are working in different departments and display with
department name.
Sol: SQL > Select name, count(*) from employee e, department d where
d.department_id=e.department_id group by name
Prob: How many employees who are working in sales department.
Sol: SQL > Select name, count(*) from employee e, department d where
d.department_id=e.department_id group by name having name=’SALES’
Prob: which is the department having greater than or equal to 5 employees and display the department name in ascending order.
Sol: SQL > Select name, count(*) from employee e, department d where
d.department_id=e.department_id group by name having count (*)>=5 order by name
Prob: How many jobs in the organization with designations.
Sol: SQL > Select function, count(*) from employee e, job j where j.job_id=e.job_id group by function
Prob: How many employees working in “New York”.
Sol: SQL > Select regional_group, count(*) from employee e, department d, location l where e.department_id=d.department_id and d.location_id=l.location_id and regional_group=’NEW YORK’ group by regional_group
Non – Equi Join:
Prob: Display employee details with salary grades.
Sol: SQL > Select employee_id, last_name, grade_id from employee e, salary_grade s where salary between lower_bound and upper_bound order by last_name
Prob: List out the no. of employees on grade wise.
Sol : SQL > Select grade_id, count(*) from employee e, salary_grade s where salary between lower_bound and upper_bound group by grade_id order by grade_id desc
Prob: Display the employee salary grades and no of employees between 2000 and 5000 of salary.
Sol: SQL > Select grade_id, count(*) from employee e, salary_grade s where salary between lower_bound and upper_bound and lower_bound>=2000 and lower_bound<=5000 group by grade_id order by grade_id desc
Self Join:
Prob: Display the employee details with their manager names.
Sol: SQL > Select e.last_name emp_name, m.last_name, mgr_name from employee e, employee m where e.manager_id=m.employee_id
Prob: Display the employee details who earn more than their managers salaries.
Sol: SQL > Select e.last_name emp_name, e.salary emp_salary, m.last_name, mgr_name, m.salary mgr_salary from employee e, employee m where e.manager_id=m.employee_id and m.salary
Prob: Show the no. of employees working under every manager.
Sol: SQL > Select m.manager_id, count(*) from employee e, employee m where
e.employee_id=m.manager_id group by m.manager_id
Outer Join:
Prob: Display employee details with all departments.
Sol: SQL > Select last_name, d.department_id, d.name from employee e, department d where e.department_id(+)=d.department_id
Prob: Display all employees in sales or operation departments.
Sol : SQL > Select last_name, d.department_id, d.name from employee e, department d where e.department_id(+)=d.department_id and d.department_idin (select department_id from department where name IN (‘SALES’,’OPERATIONS’))
Set Operators:
Prob: List out the distinct jobs in Sales and Accounting Departments.
Sol : SQL > Select function from job where job_id in (Select job_id from employee where department_id=(select department_id from department where name=’SALES’)) union Select function from job where job_id in (Select job_id from employee where department_id=(select department_id from department where name=’ACCOUNTING’))
Prob: List out the ALL jobs in Sales and Accounting Departments.
Sol: SQL > Select function from job where job_id in (Select job_id from employee where department_id=(select department_id from department where name=’SALES’)) union all Select function from job where job_id in (Select job_id from employee where department_id=(select department_id from department where name=’ACCOUNTING’))
Prob: List out the common jobs in research and accounting departments in ascending orders.
Sol: SQL > Select function from job where job_id in (Select job_id from employee where department_id=(select department_id from department where name=’RESEARCH’)) intersect Select function from job where job_id in (Select job_id from employee where department_id= (select department_id from department where name=’ACCOUNTING’)) order by function