Didn't get the answer.
Queries based on the above tables: Simple Queries: 1)List all the employee detailsSELECT * FROM EMPLOYEE
Select * from department Select * from job; 4)List all the locations Select * from loc;
Select first_name, last_name, salary, commission from employee;
Select employee_id “id of the employee”, last_name “name", department id as “department id” from employee;
Select last_name, salary*12 “annual salary” from employee Where Conditions: 8) List the details about “SMITH” select * from employee where last_name='smith';
select * from employee where dept=20; select * from employee where salary between 3000 and 4500; 11) List out the employees who are working in department 10 or 20 select * from employee where dept_id in (10,20);
select * from employee where dept_id not in (10,30) ; 13) List out the employees whose name starts with “S” select * from employee where last_nameis like 's%';
select * from employee where last_name like 'S%H'; select * from employee where last_name like 's____'; select * from employee where dept_id=10 and salary >3500; 17) list out the employees who are not receiving commission. select * from employee where comm is null; Order By Clause: 18) List out the employee id, last name in ascending order based on the employee id. select emp_id,last_name from employee order by emp_id;
select emp_id,Last_name,salary from employee order by salary desc; 20) List out the employee details according to their last_name in ascending order and salaries in descending order Select * from employee order by last_name,salary desc;
select * from employee order by last_name,dept_id desc; Group By & Having Clause: 22) How many employees who are working in different departments wise in the organizationselect dept_id,count(*) from employee group by dept_id.
select dept_id,count(*),max(salary),min(salary),avg(salary) from employee group by dept_id
select job ,salary from employee count(*),max(salary),min(salary),avg(salary);
Select to_char(hire_date,’month’)month, count(*) from employee group by to_char(hire_date,’month’) order by month Convert a numeric or date expression to a character String. Syntax to_char(expression [,'format'] [,nls_format]) Key char The DATE, NUMBER or expression to convert format Format to use. nls_lang The international language to use.The format may be either a DATE format (YYYY=year, MM=month, DD=Day, HH=Hour, Mi=Minute ) If no format is specified Oracle will use the default date format. nls_format allows international formats to be applied. TO_CHAR will convert NCHAR, NVARCHAR2, CLOB, or NCLOB data to the database character set. Examples SQL> Select to_char(sysdate, 'yyyy/mm/dd') FROM dual; '2010/12/24' SQL> Select to_char(sysdate, 'FMMonth DD, YYYY') FROM dual; 'June 9, 2005' SQL> select to_char(sysdate,'HH24:MI:SS') "Time Now" from dual; '14:35:56'26) List out the no.of employees for each month and year, in the ascending order based on the year, month. select to_char(hire_date,'month')month,to_char(hire_date,'year')year,count(*) "no. of employees" from employee group by to_char(hire_date,'year')year,to_char(hire_date,'month')month orer by year,month; 27) List out the department id having atleast four employees. select dept_id,count(*) from employee group by dept_id having count(*)>=4; 28) How many employees in January month. select to_char(hire_date,'month')month,count(*) from employee group by to_char(hire_date,'month') having to_char(hire_date,'month') ='jan';
select to_char(hire_date,'month')month,count(*) from employee group by to_char(hire_date,'month') having to_char(hire_date,'month') in ('jan,'sep'); 30) How many employees who are joined in 1985. select to_char(hire_date,'yyyy')year,count(*) from employee group by to_char(hire_date,'yyyy') having to_char(hire_date,'yyyy')= 1985;
select to_char(hire_date,'yyyy')year,to_char(hire_date,'month')month ,count(*) from employee group by to_char(hire_date,'yyyy') ,to_char(hire_date,'month')month having to_char(hire_date,'yyyy')= 1985; 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’) The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.
select to_char(hire_date,'yyyy')year,to_char(hire_date,'month')month ,count(*) from employee group by to_char(hire_date,'yyyy') ,to_char(hire_date,'month')month having to_char(hire_date,'yyyy')= 1985 ,to_char(hire_date,'moth')='mar'; 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’) 33) Which is the department id, having greater than or equal to 3 employees joined in April 1985.select dept_id ,count(*) from employee group by dept_id,to_char(hire_date,'yyyy'),to_char(hire_date,'month') having count(*)>.=3,to_char(hire_date,'yyyy')=1985,to_char(hire_date,'month')='april'; 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 34) Display the employee who got the maximum salary select * from employee where salary=(selct max(salary) from employee;
select* from employee where dept_id in(select dept_id from department where department='sales'); select * from employee where job_id in (selct job_id from job where function='clerk');
select * from employee where dept_id=(select dept_id from depatment where location id= (select location_id from location where regional_group='newyork');
select count(*) from employee where dept_id in(select dept_id from department where name ='sales); upadate employee set salary=salary*10/100 where job_id=(selct job_id from job where function='clerk'); delete from employee where dept_id=(select dept_id from department where name='accounting');
select * from employee where salary=(selct max(salary) from employee where salary<(select max(salary) from employee));
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) 43) List out the employees who earn more than every employee in department 30.select * from employee where salary>all(select salary from employee where dept_id=30);
select * from employee where salray>any(select salry from employee where dept_id=30); select * from employee e where not exists(selct dept_id from department d where d.dept_id=e.dept_id);
select dept_id from employee where dept_id=!(select dept_id from department); 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: 47) .Find out the employees who earn greater than the average salary for their department. select * from employee e where salary>(select avg(salary) from employee where dept_id=e.dept_id); Simple join 48.List our employees with their department names select last_name,dept_id,name, salary from employee e,dept d where e.dept_id=d.dept_id; Select employee_id, last_name, name from employee e, department d where e.department_id=d.department_id 49).Display employees with their designations (jobs) select last_name,job_id,function from employee e ,job j where j.job_id=e.job_id; 50).Display the employees with their department name and regional groups. select last_name, dept_id,location _id,regional _group from department d,employee e ,location l where e.dept_id=d.dept_id and d.location_id=l.location_id; 51). How many employees who are working in different departments and display with department name. select count (*) from employee e depatment d where e.dept_id=e.dept_id group by name 52.How many employees who are working in sales department. select count(*) from employee e,department d where e.dept_id=d.dept_id group by name having name='sales'; 53.Which is the department having greater than or equal to 5 employees and display the department names in ascending order. select count(*) dept_id,name from employee e,department d where e.dept_id=d.dept_id group by name having count(*)>=5 order by name 54.How many jobs in the organization with designations. select count(*) from employee e,job j e.job_id=j.job_id group by function; 55.How many employees working in “New York”. select count(*) ,location_id,dept_id from employee e,location l,department d where e.dept_id=d.dept_id and dept_id=(select dept_id from depatment where location_id=(select loaction_id from location where regional_group='new york'))) select count(*) from employee e,department d,location l where e.dept_id=d.dept_id ,d.location_id=l.location_id where regional_group='newyork'group by regional group; Non – Equi Join: 56.Display employee details with salary grades. 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 57.List out the no. of employees on grade wise. select count(*) ,grade_id from employee salary_grade s where salary between lower_bound and upper_bound group by grade_id order by grade_id desc 58.Display the employ salary grades and no. of employees between 2000 to 5000 range of salary. select count(*), grade_id from employee e ,salary_grade s where salary between lower_bound and upper_bound and lower_bound>= 2000 and upper_bound<5000 group by grade Self Join: 59.Display the employee details with their manager names. select last_name job_id ,function from employee e ,job j where e.job_id=j.job_id and job_id=(select job_id where function='manager') if we consider there is another table manager select last_name ,manager_id,name from employee e ,manager m where e.manager_id=m.manager_id group by name 60.Display the employee details who earn more than their managers salaries. 61.Show the no. of employees working under every manager. Outer Join: 62.Display employee details with all departments. select * from employee e , department d where e.dept_id=d.dept_id group by dept_id 63.Display all employees in sales or operation departments. select * from employee e depatment d where e.dept_id=d.dept_id and dept_id=(select dept_id where name in('sales' ,'operations'); Set Operators: 64.List out the distinct jobs in Sales and Accounting Departments. select function from job where job_id in(select job_id from employee where dept_id=(select dept_id where name='sales'))union select function from job where job_id in(select job_id from employee where dept_id=(select dept_id where name='accounting')) 65.List out the ALL jobs in Sales and Accounting Departments. select function from job where job_id in(select job_id from employee where dept_id=(select dept_id where name='sales'))union all select function from job where job_id in(select job_id from employee where dept_id=(select dept_id where name='accounting')) 66.List out the common jobs in Research and Accounting Departments in ascending order. select function from job where job_id in(select job_id from employee where dept_id=(select dept_id where name='reasearch'))intersect select function from job where job_id in(select job_id from employee where dept_id=(select dept_id where name='accounting')) Answers
SQL Queries examples - |