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

SQL Queries

SQL Queries in detail

Uploaded by

shubhangi.ladde
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

SQL Queries

SQL Queries in detail

Uploaded by

shubhangi.ladde
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 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,...)

 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

Department_Id Name Location_Id


10 Accounting 122
20 Research 124
30 Sale 123
40 Operation 167

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.

Employee Lastnam Firstnam Middlena Job_Id Manager Hiredate Salary Departm


_Id e e me _id ent_id
7369 Smith Jon Q 667 7902 17-DEC- 800 10
84
7499 Allen Kevin J 670 7698 20-FEB- 1600 20
85
7505 Doyle Jean K 671 7839 04-APR- 2850 20
85
7506 Dennis Lynn S 671 7839 15-MAY- 2750 30
85
7507 Baker Leslie D 671 7839 10-JUN- 2200 40
85
7521 wark cynthia D 670 7698 22-FEB- 1250 10
85

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

Department_Id Name Location_Id


10 Accounting 122
20 Research 124
30 Sale 123
40 Operation 167

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.

Employee Lastnam Firstnam Middlena Job_Id Manager Hiredate Salary Departm


_Id e e me _id ent_id
7369 Smith Jon Q 667 7902 17-DEC- 800 10
84
7499 Allen Kevin J 670 7698 20-FEB- 1600 20
85
7505 Doyle Jean K 671 7839 04-APR- 2850 20
85
7506 Dennis Lynn S 671 7839 15-MAY- 2750 30
85
7507 Baker Leslie D 671 7839 10-JUN- 2200 40
85
7521 wark cynthia D 670 7698 22-FEB- 1250 10
85

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

You might also like