SQL Lab Task 3
SQL Lab Task 3
+-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------
+------------+---------------+
+-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------
+------------+---------------+
+-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------
+------------+---------------+
Sample table: Department
+---------------+----------------------+------------+-------------+
+---------------+----------------------+------------+-------------+
| 60 | IT | 103 | 1400 |
+---------------+----------------------+------------+-------------+
+-------------+------------------------------------------+-------------+---------------------+-------------------+------------+
+-------------+------------------------------------------+-------------+---------------------+-------------------+------------+
| 2500 | Magdalen Centre, The Oxford Science Park | OX9 9ZB | Oxford | Oxford |
UK |
| 2800 | Rua Frei Caneca 1360 | 01307-002 | Sao Paulo | Sao Paulo | BR
|
+-------------+------------------------------------------+-------------+---------------------+-------------------+------------+
1. From the following table, write a SQL query to find those employees who receive a higher salary than
the employee with ID 163. Return first name, last name.
Sample Output:
first_name last_name
Steven King
Neena Kochhar
Lex De Haan
.....
2. From the following table, write a SQL query to find out which employees have the same designation
as the employee whose ID is 169. Return first name, last name, department ID and job ID.
Sample Output:
.....
3. From the following table, write a SQL query to find those employees whose salary matches the lowest
salary of any of the departments. Return first name, last name and department ID.
Sample Output:
.....
4. From the following table, write a SQL query to find those employees who earn more than the average
salary. Return employee ID, first name, last name.
Sample Output:
.....
5. From the following table, write a SQL query to find those employees who report to that manager
whose first name is ‘Payam’. Return first name, last name, employee ID and salary.
Sample Output:
.....
6. From the following tables, write a SQL query to find all those employees who work in the Finance
department. Return department ID, name (first), job ID and department name.
Sample Output:
7. From the following table, write a SQL query to find the employee whose salary is 3000 and reporting
person’s ID is 121. Return all fields.
Sample Output:
8. From the following table, write a SQL query to find those employees whose ID matches any of the
numbers 134, 159 and 183. Return all the fields.
Sample Output:
9. From the following table, write a SQL query to find those employees whose salary is in the range of
1000, and 3000 (Begin and end values have included.). Return all the fields.
Sample Output:
.....
10. From the following table and write a SQL query to find those employees whose salary falls within the
range of the smallest salary and 2500. Return all the fields.
Sample Output:
.....
11. From the following tables, write a SQL query to find those employees who do not work in the
departments where managers’ IDs are between 100 and 200 (Begin and end values are included.).
Return all the fields of the employeess.
Sample Output:
12. From the following table, write a SQL query to find those employees who get second-highest salary.
Return all the fields of the employees.
Sample Output:
13. From the following tables, write a SQL query to find those employees who work in the same
department as ‘Clara’. Exclude all those records where first name is ‘Clara’. Return first name, last name
and hire date.
Sample Output:
.....
14. From the following tables, write a SQL query to find those employees who work in a department
where the employee’s first name contains the letter 'T'. Return employee ID, first name and last name.
Sample Output:
.....
15. From the following tables, write a SQL query to find those employees who earn more than the
average salary and work in the same department as an employee whose first name contains the letter
'J'. Return employee ID, first name and salary.
Sample Output:
.....
16. From the following table, write a SQL query to find those employees whose department is located at
‘Toronto’. Return first name, last name, employee ID, job ID.
Sample Output:
17. From the following table, write a SQL query to find those employees whose salary is lower than that
of employees whose job title is ‘MK_MAN’. Return employee ID, first name, last name, job ID.
Sample Output:
.....
18. From the following table, write a SQL query to find those employees whose salary is lower than that
of employees whose job title is "MK_MAN". Exclude employees of Job title ‘MK_MAN’. Return employee
ID, first name, last name, job ID.
Sample Output:
.....
19. From the following table, write a SQL query to find those employees whose salary exceeds the salary
of all those employees whose job title is "PU_MAN". Exclude job title ‘PU_MAN’. Return employee ID,
first name, last name, job ID.
Sample Output:
.....
20. From the following table, write a SQL query to find those employees whose salaries are higher than
the average for all departments. Return employee ID, first name, last name, job ID.
Sample Output:
21. From the following table, write a SQL query to check whether there are any employees with salaries
exceeding 3700. Return first name, last name and department ID.
Sample Output:
Steven King 90
Neena Kochhar 90
Lex De Haan 90
Alexander Hunold 60
Bruce Ernst 60
.....
22. From the following table, write a SQL query to calculate total salary of the departments where at
least one employee works. Return department ID, total salary.
Sample Output:
department_id total_amt
10 4400.00
20 19000.00
30 24900.00
40 6500.00
.....
23. Write a query to display the employee id, name ( first name and last name ) and the job id column
with a modified title SALESMAN for those employees whose job title is ST_MAN and DEVELOPER for
whose job title is IT_PROG.
Sample Output:
24. Write a query to display the employee id, name ( first name and last name ), salary and the
SalaryStatus column with a title HIGH and LOW respectively for those employees whose salary is more
than and less than the average salary of all employees.
Sample Output:
employee_id first_name last_name salary salarystatus
25. Write a query to display the employee id, name ( first name and last name ), SalaryDrawn,
AvgCompare (salary - the average salary of all employees) and the SalaryStatus column with a title HIGH
and LOW respectively for those employees whose salary is more than and less than the average salary of
all employees.
Sample Output:
26. From the following table, write a SQL query to find all those departments where at least one
employee is employed. Return department name.
Sample Output:
department_name
Administration
Marketing
Purchasing
Human Resources
Shipping
.....
27. From the following tables, write a SQL query to find employees who work in departments located in
the United Kingdom. Return first name.
Sample Output:
first_name
Susan
28. From the following table, write a SQL query to find out which employees are earning more than the
average salary and who work in any of the IT departments. Return last name.
Sample Output:
last_name
Hunold
29. From the following table, write a SQL query to find all those employees who earn more than an
employee whose last name is 'Ozer'. Sort the result in ascending order by last name. Return first name,
last name and salary.
Sample Output:
30. From the following tables, write a SQL query find the employees who report to a manager based in
the United States. Return first name, last name.
Sample Output:
first_name last_name
Neena Kochhar
Lex De Haan
Alexander Hunold
Bruce Ernst
David Austin
.....
Joins
Sample Table:Order
ord_no purch_amt ord_date customer_id salesman_id
---------- ---------- ---------- ----------- -----------
70001 150.5 2012-10-05 3005 5002
70009 270.65 2012-09-10 3001 5005
70002 65.26 2012-10-05 3002 5001
70004 110.5 2012-08-17 3009 5003
70007 948.5 2012-09-10 3005 5002
70005 2400.6 2012-07-27 3007 5001
70008 5760 2012-09-10 3002 5001
70010 1983.43 2012-10-10 3004 5006
70003 2480.4 2012-10-10 3009 5003
70012 250.45 2012-06-27 3008 5002
70011 75.29 2012-08-17 3003 5007
70013 3045.6 2012-04-25 3002 5001
Sample table: Customer
customer_id | cust_name | city | grade | salesman_id
-------------+----------------+------------+-------+-------------
3002 | Nick Rimando | New York | 100 | 5001
3007 | Brad Davis | New York | 200 | 5001
3005 | Graham Zusi | California | 200 | 5002
3008 | Julian Green | London | 300 | 5002
3004 | Fabian Johnson | Paris | 300 | 5006
3009 | Geoff Cameron | Berlin | 100 | 5003
3003 | Jozy Altidor | Moscow | 200 | 5007
3001 | Brad Guzan | London | | 5005
Sample Table: Salesman
salesman_id | name | city | commission
-------------+------------+----------+------------
5001 | James Hoog | New York | 0.15
5002 | Nail Knite | Paris | 0.13
5005 | Pit Alex | London | 0.11
5006 | Mc Lyon | Paris | 0.14
5007 | Paul Adam | Rome | 0.13
5003 | Lauson Hen | San Jose | 0.12
1. From the following tables write a SQL query to find the salesperson and customer who
reside in the same city. Return Salesman, cust_name and city.
2. From the following tables write a SQL query to find those orders where the order
amount exists between 500 and 2000. Return ord_no, purch_amt, cust_name, city.
3. From the following tables write a SQL query to find the salesperson(s) and the
customer(s) he represents. Return Customer Name, city, Salesman, commission.
4. From the following tables write a SQL query to find salespeople who received
commissions of more than 12 percent from the company. Return Customer Name,
customer city, Salesman, commission.
5. From the following tables write a SQL query to locate those salespeople who do not live
in the same city where their customers live and have received a commission of more than
12% from the company. Return Customer Name, customer city, Salesman, salesman city,
commission.
6. From the following tables write a SQL query to find the details of an order. Return
ord_no, ord_date, purch_amt, Customer Name, grade, Salesman, commission.
7. Write a SQL statement to join the tables salesman, customer and orders so that the same
column of each table appears once and only the relational rows are returned.
8. From the following tables write a SQL query to display the customer name, customer
city, grade, salesman, salesman city. The results should be sorted by ascending
customer_id.
9. From the following tables write a SQL query to find those customers with a grade less
than 300. Return cust_name, customer city, grade, Salesman, salesmancity. The result
should be ordered by ascending customer_id.
10. Write a SQL statement to make a report with customer name, city, order number,
order date, and order amount in ascending order according to the order date to determine
whether any of the existing customers have placed an order or not.
11. SQL statement to generate a report with customer name, city, order number, order
date, order amount, salesperson name, and commission to determine if any of the existing
customers have not placed orders or if they have placed orders through their salesman or
by themselves.
12. Write a SQL statement to generate a list in ascending order of salespersons who work
either for one or more customers or have not yet joined any of the customers.
13. From the following tables write a SQL query to list all salespersons along with customer
name, city, grade, order number, date, and amount.
14. Write a SQL statement to make a list for the salesmen who either work for one or more
customers or yet to join any of the customer. The customer may have placed, either one or
more orders on or above order amount 2000 and must have a grade, or he may not have
placed any order to the associated supplier.
15.For those customers from the existing list who put one or more orders, or which orders
have been placed by the customer who is not on the list, create a report containing the
customer name, city, order number, order date, and purchase amount
16. Write a SQL statement to generate a report with the customer name, city, order no.
order date, purchase amount for only those customers on the list who must have a grade
and placed one or more orders or which order(s) have been placed by the customer who
neither is on the list nor has a grade.
17. Write a SQL query to combine each row of the salesman table with each row of the
customer table.
18. Write a SQL statement to create a Cartesian product between salesperson and
customer, i.e. each salesperson will appear for all customers and vice versa for that
salesperson who belongs to that city.
19. Write a SQL statement to create a Cartesian product between salesperson and
customer, i.e. each salesperson will appear for every customer and vice versa for those
salesmen who belong to a city and customers who require a grade.
20. Write a SQL statement to make a Cartesian product between salesman and customer
i.e. each salesman will appear for all customers and vice versa for those salesmen who must
belong to a city which is not the same as his customer and the customers should have their
own grade.
COM_ID COM_NAME
------ -------------
11 Samsung
12 iBall
13 Epsion
14 Zebronics
15 Asus
16 Frontech
21. From the following tables write a SQL query to select all rows from both participating
tables as long as there is a match between pro_com and com_id.
22. Write a SQL query to display the item name, price, and company name of all the
products.
23. From the following tables write a SQL query to calculate the average price of items of
each company. Return average value and company name.
57 IT 65000
63 Finance 15000
47 HR 240000
27 RD 55000
89 QC 75000
26. From the following tables write a SQL query to display all the data of employees
including their department.
27. From the following tables write a SQL query to display the first and last names of each
employee, as well as the department name and sanction amount.
29. From the following tables write a SQL query to find the names of departments where
more than two employees are employed. Return dpt_name.