You are on page 1of 18

SQL Queries

By KBS

The create table statement


CREATE TABLE <table name> ( <attribute name 1> <data type 1>, <attribute name 2>

<data type 2> . ... <attribute name n> <data type n>);

The Insert statement


Two ways 1) INSERT INTO table_name

VALUES (value1, value2, value3,...)


2)INSERT INTO table_name (column1, column2,

column3,...)VALUES (value1, value2, value3,...)


By KBS

Create table Location as follows


Location(Location_Id, Reginal_Group) Insert the following records
Location_Id Reginal_Group

122 123 124 125

New York Dallas Chicago Boston

By KBS

Create table

Department(Department_Id,Name,Location_Id) Insert following records


Department_I d 10 20 30 40 Name
Accounting

Location_Id
122

Research Sale Operation

124 123 167

By KBS

Create table

Job(Job_Id,Function) Insert following records


Job_Id 667 668 669 670 671 672 Function Cleark Staff Analyst Saleperson Manager President

By KBS

Create table

Employee(Employee_Id,Lastname,Firstname,Middlename,Job_Id, Manager_id,Hiredate,Salary,Department_id) Insert following records.


Employe e_Id
7369 7499 7505 7506 7507 7521

Lastna me
Smith Allen Doyle Dennis Baker wark

Firstna me
Jon Kevin Jean Lynn Leslie cynthia

Middlen ame
Q J K S D D

Job_Id
667 670 671 671 671 670

Manager _id
7902 7698 7839 7839 7839 7698

Hiredate
17-DEC84 20-FEB85 04-APR85 15-MAY85 10-JUN85 22-FEB85

Salary
800 1600 2850 2750 2200 1250

Departm ent_id
10 20 20 30 40 10

By KBS

Create table Location as follows


Location(Location_Id, Reginal_Group) with

Location_Id as primary key Insert the following records


Location_Id Reginal_Group

122 123

New York Dallas

124 167

Chicago Boston

By KBS

Create table

Department(Department_Id,Name,Location_Id) with Department_Id as primary Key and Location_Id as a forign key refers Location_Id in Location table Insert following records
Department_I d 10 20 30 Name Accounting Research Sale Location_Id 122 124 123

40

Operation

167

By KBS

Create table

Job(Job_Id,Function) with job_Id as

primary key Insert following records


Job_Id 667 668 669 670 671 672 Function Cleark Staff Analyst Saleperson Manager President

By KBS

Create table

Employee(Employee_Id,Lastname,Firstname,Middlename,Job_Id, Manager_id,Hiredate,Salary,Department_id) With Employee_id as primary Key , Job_id,Department_id as foreign key Insert following records.
Employe e_Id 7369 7499 7505 7506 7507 7521 Lastna me Smith Allen Doyle Dennis Baker wark By KBS Firstna me Jon Kevin Jean Lynn Leslie cynthia Middlen ame Q J K S D D Job_Id 667 670 671 671 671 670 Manager _id 7902 7698 7839 7839 7839 7698 Hiredate 17-DEC84 20-FEB85 04-APR85 15-MAY85 10-JUN85 22-FEB85 Salary 800 1600 2850 2750 2200 1250 Departm ent_id 10 20 20 30 40 10

QUERIES BASED ON ABOVE TABLE


Simple Queries
1. 2. 3. 4. 5. 6.

7.

List all the employee details. List all the department details. List all job details. List all the locations. List out first name , last name , salary , for all employees. List out employee_id , last name,department_id for all employuees and rename employee id as ID of the Employee, last name as Name of the Employee, department id as Deparment ID. List out the employees annual salary with their names only. By KBS

Where conditions:
8. List the details about smith
9. List out the employee whose job id is 671. 10. List out the employees who are earning salary between

3000 and 4500. 11. List out the employee who are working in department 10 or 20. 12. Find out the employees who are not working in department 10 or 30. 13. List out the employees whose name start with s. 14. List out the employees whose name start with s and end with h. 15. List out the employees whose name length is 4 and start with s. 16. By List KBS out the employees who are working in department 10 and draw the salaries more than 1000.

Order By Clause:
17. List out employee id, last name in ascending order

based on the employee id. 18. List out employee id, last name in descending order based on the salary column. 19. List out employee details according to their last name in ascending order and salaries in descending order. 20. List out employee details according to their last name in ascending order and then on department_id in descending order.

By KBS

Group By & Having Clause:


21. How many employees who are working in different departments
22. 23. 24. 25.

26. 27. 28. 29. 30. 31. 32.

wise in the organization List out the department wise maximum salary, minimum salary, average salary of the employees List out the job wise maximum salary, minimum salary, average salaries of the employees. List out the no.of employees joined in every month in ascending order. List out the no.of employees for each month and year, in the ascending order based on the year, month. List out the department id having atleast two employees. How many employees in feb month. How many employees who are joined in feb or may month. How many employees who are joined in 1985. How many employees joined each month in 1985. How many employees who are joined in Feb 1985. Which is the department id, having greater than or equal to 1 employees joined in feb 1985.
By KBS

Sub-Queries
33. Display the employee who got the maximum salary. 34. Display the employees who are working in Sales

35.
36. 37. 38. 39. 40.

department Display the employees who are working as Cleark. Display the employees who are working in New York Find out no.of employees working in Sales department. Update the employees salaries, who are working as Clerk on the basis of 10%. Delete the employees who are working in accounting department. Display the second highest salary drawing employee details.

By KBS

Sub-Query operators: (ALL,ANY,SOME,EXISTS)


41. List out the employees who earn more than every employee
42. 43. 44. 45.

in department 30. List out the employees who earn more than the lowest salary in department 30. Find out whose department has not employees. Find out which department does not have any employees. Find out the employees who earn greater than the average salary for their department.

By KBS

Joins
Simple join 46. List our employees with their department names 47. Display employees with their designations (jobs) 48. Display the employees with their department name and 49. 50. 51.

52.

53.

regional groups. How many employees who are working in different departments and display with department name. How many employees who are working in sales department. Which is the department having greater than or equal to 2 employees and display the department names in ascending order. How many jobs in the organization with designations. By KBS How many employees working in Chicago.

Set Operators: 54. List out the distinct jobs in Sales and Accounting Departments. 55. List out the ALL jobs in Sales and Accounting Departments. 56. List out the common jobs in Sale and Research Departments in ascending order.

By KBS

You might also like