SQL Queries
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>);
To load the text file pet.txt into the pet table, use this statement:
mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet;
By KBS
Create table Location as follows
Location(Location_Id, Reginal_Group)
Insert the following records
Location_Id Reginal_Group
122 New York
123 Dallas
124 Chicago
167 Boston
By KBS
Create table
Department(Department_Id,Name,Location_Id)
Insert following records
By KBS
Create table Job(Job_Id,Function)
Insert following records
Job_Id Function
667 Cleark
668 Staff
669 Analyst
670 Saleperson
671 Manager
672 President
By KBS
Create table
Employee(Employee_Id,Lastname,Firstname,Middlename,Job_Id,Manag
er_id,Hiredate,Salary,Department_id)
Insert following records.
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 New York
123 Dallas
124 Chicago
167 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
By KBS
Create table Job(Job_Id,Function) with job_Id as
primary key
Insert following records
Job_Id Function
667 Cleark
668 Staff
669 Analyst
670 Saleperson
671 Manager
672 President
By KBS
Create table
Employee(Employee_Id,Lastname,Firstname,Middlename,Job_Id,Manag
er_id,Hiredate,Salary,Department_id)
With Employee_id as primary Key , Job_id,Department_id as foreign key
Insert following records.
By KBS
QUERIES BASED ON ABOVE TABLE
Simple Queries
1. List all the employee details.
2. List all the department details.
3. List all job details.
4. List all the locations.
5. List out first name , last name , salary , for all employees.
6. 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”.
7. 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. List out the employees who are working in department 10 and
draw the salaries more than 1000.
By KBS
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 wise in the
organization
22. List out the department wise maximum salary, minimum salary, average
salary of the employees
23. List out the job wise maximum salary, minimum salary, average salaries of
the employees.
24. List out the no.of employees joined in every month in ascending order.
25. List out the no.of employees for each month and year, in the ascending order
based on the year, month.
26. List out the department id having atleast two employees.
27. How many employees in feb month.
28. How many employees who are joined in feb or may month.
29. How many employees who are joined in 1985.
30. How many employees joined each month in 1985.
31. How many employees who are joined in Feb 1985.
32. 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 department
35. Display the employees who are working as “Cleark”.
36. Display the employees who are working in “New York”
37. Find out no.of employees working in “Sales” department.
38. Update the employees salaries, who are working as Clerk on
the basis of 10%.
39. Delete the employees who are working in accounting
department.
40. 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 in
department 30.
42. List out the employees who earn more than the lowest salary in
department 30.
43. Find out whose department has not employees.
44. Find out which department does not have any employees.
45. 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
regional groups.
49. How many employees who are working in different
departments and display with department name.
50. How many employees who are working in sales department.
51. Which is the department having greater than or equal to 2
employees and display the department names in ascending
order.
52. How many jobs in the organization with designations.
53. How many employees working in “Chicago”.
By KBS
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