MySQL Assignment -3 (DQL)
Q1. Write a query to display the names (first_name, last_name) using alias name
“First Name", "Last Name".
mysql> select * from Employees;
+------+------------+-----------+-----------+--------------+------------
+------------+--------+-----------+------------+---------------+
| eid | First_name | last_name | email | phone_no | job_id | hire_date
| salary | comission | MANAGER_ID | DEPARTMENT_ID |
+------+------------+-----------+-----------+--------------+------------
+------------+--------+-----------+------------+---------------+
| 101 | Steven | King | SKING | 515.123.4567 | AD_PRES | 1998-06-
17 | 2400 | 0 | 0 | 90 |
| 102 | Lex | De Haan | LEDEHAAN | 515.123.4569 | AD_VP | 1987-06-
19 | 17000 | 0 | 100 | 90 |
| 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | IT_PROG | 1987-06-
20 | 9000 | 0 | 103 | 60 |
| 104 | Bruce | Ernst | BERNST | 590.423.4568 | IT_PROG | 1987-06-
21 | 6000 | 0 | 103 | 60 |
| 105 | David | Austin | DAUSTIN | 590.423.4569 | IT_PROG | 1987-06-
22 | 4800 | 0 | 103 | 60 |
| 106 | Valli | Pataballa | VAPATABAL | 590.432.4560 | IT_PROG | 1987-06-
23 | 4800 | 0 | 103 | 60 |
| 107 | Dinna | Lorentz | DLORENTZ | 590.423.5567 | IT_POOG | 1987-06-
24 | 4200 | 0 | 103 | 60 |
| 108 | Nancy | Greenberg | NEREENBE | 515.124.4569 | FI_MGR | 1987-06-
25 | 12000 | 0 | 101 | 100 |
| 109 | Daniel | Faviet | DFAVIET | 515.124.4169 | FI_ACCOUNT | 1987-06-
26 | 9000 | 0 | 108 | 100 |
| 100 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | AD_VP | 1997-06-
18 | 18700 | 0 | 100 | 90 |
+------+------------+-----------+-----------+--------------+------------
+------------+--------+-----------+------------+---------------+
Query -
mysql> select First_name "First Name", last_name "last Name" from Employees;
+------------+-----------+
| First Name | last Name |
+------------+-----------+
| Steven | King |
| Lex | De Haan |
| Alexander | Hunold |
| Bruce | Ernst |
| David | Austin |
| Valli | Pataballa |
| Dinna | Lorentz |
| Nancy | Greenberg |
| Daniel | Faviet |
| Neena | Kochhar |
+------------+-----------+
10 rows in set (0.00 sec)
___________________________________________________________________________________
___________________________________________________________________________________
___________
___________________________________________________________________________________
___________________________________________________________________________________
___________
Q2. Write a query to get unique department ID from employee table.
Query -
mysql> select distinct DEPARTMENT_ID FROM Employees ;
+---------------+
| DEPARTMENT_ID |
+---------------+
| 90 |
| 60 |
| 100 |
+---------------+
3 rows in set (0.16 sec)
___________________________________________________________________________________
___________________________________________________________________________________
_______
___________________________________________________________________________________
___________________________________________________________________________________
_______
Q3. Write a query to get all employee details from the employee table order by
first name, descending.
Query -
mysql> select * from Employees order by First_name desc;
+------+------------+-----------+-----------+--------------+------------
+------------+--------+-----------+------------+---------------+
| eid | First_name | last_name | email | phone_no | job_id | hire_date
| salary | comission | MANAGER_ID | DEPARTMENT_ID |
+------+------------+-----------+-----------+--------------+------------
+------------+--------+-----------+------------+---------------+
| 106 | Valli | Pataballa | VAPATABAL | 590.432.4560 | IT_PROG | 1987-06-
23 | 4800 | 0 | 103 | 60 |
| 101 | Steven | King | SKING | 515.123.4567 | AD_PRES | 1998-06-
17 | 2400 | 0 | 0 | 90 |
| 100 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | AD_VP | 1997-06-
18 | 18700 | 0 | 100 | 90 |
| 108 | Nancy | Greenberg | NEREENBE | 515.124.4569 | FI_MGR | 1987-06-
25 | 12000 | 0 | 101 | 100 |
| 102 | Lex | De Haan | LEDEHAAN | 515.123.4569 | AD_VP | 1987-06-
19 | 17000 | 0 | 100 | 90 |
| 107 | Dinna | Lorentz | DLORENTZ | 590.423.5567 | IT_POOG | 1987-06-
24 | 4200 | 0 | 103 | 60 |
| 105 | David | Austin | DAUSTIN | 590.423.4569 | IT_PROG | 1987-06-
22 | 4800 | 0 | 103 | 60 |
| 109 | Daniel | Faviet | DFAVIET | 515.124.4169 | FI_ACCOUNT | 1987-06-
26 | 9000 | 0 | 108 | 100 |
| 104 | Bruce | Ernst | BERNST | 590.423.4568 | IT_PROG | 1987-06-
21 | 6000 | 0 | 103 | 60 |
| 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | IT_PROG | 1987-06-
20 | 9000 | 0 | 103 | 60 |
+------+------------+-----------+-----------+--------------+------------
+------------+--------+-----------+------------+---------------+
10 rows in set (0.08 sec)
___________________________________________________________________________________
___________________________________________________________________________________
___
___________________________________________________________________________________
___________________________________________________________________________________
___
Q4. Write a query to get the names (first_name, last_name), salary, PF of all the
employees (PF is calculated as 15% of salary).
Query -
mysql> select First_name , last_name, salary, salary * 0.15 PF from Employees;
+------------+-----------+--------+------+
| First_name | last_name | salary | PF |
+------------+-----------+--------+------+
| Steven | King | 2400 | 360 |
| Lex | De Haan | 17000 | 2550 |
| Alexander | Hunold | 9000 | 1350 |
| Bruce | Ernst | 6000 | 900 |
| David | Austin | 4800 | 720 |
| Valli | Pataballa | 4800 | 720 |
| Dinna | Lorentz | 4200 | 630 |
| Nancy | Greenberg | 12000 | 1800 |
| Daniel | Faviet | 9000 | 1350 |
| Neena | Kochhar | 18700 | 2805 |
+------------+-----------+--------+------+
10 rows in set (0.09 sec)
___________________________________________________________________________________
___________________________________________________________________________________
____
___________________________________________________________________________________
___________________________________________________________________________________
____
Q5. Write a query to get the employee ID, names (first_name, last_name), salary in
ascending order of salary.
Query -
mysql> select eid, First_name, last_name, salary from Employees order by salary;
+------+------------+-----------+--------+
| eid | First_name | last_name | salary |
+------+------------+-----------+--------+
| 101 | Steven | King | 2400 |
| 107 | Dinna | Lorentz | 4200 |
| 105 | David | Austin | 4800 |
| 106 | Valli | Pataballa | 4800 |
| 104 | Bruce | Ernst | 6000 |
| 103 | Alexander | Hunold | 9000 |
| 109 | Daniel | Faviet | 9000 |
| 108 | Nancy | Greenberg | 12000 |
| 102 | Lex | De Haan | 17000 |
| 100 | Neena | Kochhar | 18700 |
+------+------------+-----------+--------+
10 rows in set (0.03 sec)
___________________________________________________________________________________
___________________________________________________________________________________
___
___________________________________________________________________________________
___________________________________________________________________________________
___
Q6. Write a query to get the total salaries payable to employees.
Query -
mysql> select sum(salary) from Employees;
+-------------+
| sum(salary) |
+-------------+
| 87900 |
+-------------+
1 row in set (0.59 sec)
___________________________________________________________________________________
___________________________________________________________________________________
______
___________________________________________________________________________________
___________________________________________________________________________________
______
Q7. Write a query to get the maximum and minimum salary from employees table.
Query -
mysql> select max(salary), min(salary) from Employees;
+-------------+-------------+
| max(salary) | min(salary) |
+-------------+-------------+
| 18700 | 2400 |
+-------------+-------------+
1 row in set (0.04 sec)
___________________________________________________________________________________
___________________________________________________________________________________
_______
___________________________________________________________________________________
___________________________________________________________________________________
________
Q8. Write a query to get the average salary and number of employees in the
employees table.
Query-
mysql> select avg(salary), count(*) from Employees;
+-------------+----------+
| avg(salary) | count(*) |
+-------------+----------+
| 8790 | 10 |
+-------------+----------+
1 row in set (0.04 sec)
___________________________________________________________________________________
___________________________________________________________________________________
______
___________________________________________________________________________________
___________________________________________________________________________________
______
Q9. Write a query to get the number of employees working with the company.
Query-
mysql> select count(*) from Employees;
+----------+
| count(*) |
+----------+
| 10 |
+----------+
1 row in set (0.44 sec)
___________________________________________________________________________________
___________________________________________________________________________________
______
___________________________________________________________________________________
___________________________________________________________________________________
______
Q 10. 10. Write a query to get the number of jobs available in the employees
table.
Query -
mysql> select count(distinct job_id) from Employees;
+------------------------+
| count(distinct job_id) |
+------------------------+
| 6 |
+------------------------+
1 row in set (0.09 sec)
___________________________________________________________________________________
___________________________________________________________________________________
_______
___________________________________________________________________________________
___________________________________________________________________________________
_______
Q11. Write a query to select first 10 records from a table.
Query -
mysql> select eid, First_name from Employees limit 10;
+------+------------+
| eid | First_name |
+------+------------+
| 101 | Steven |
| 102 | Lex |
| 103 | Alexander |
| 104 | Bruce |
| 105 | David |
| 106 | Valli |
| 107 | Dinna |
| 108 | Nancy |
| 109 | Daniel |
| 100 | Neena |
+------+------------+
10 rows in set (0.00 sec)
___________________________________________________________________________________
___________________________________________________________________________________
______
___________________________________________________________________________________
___________________________________________________________________________________
______
Q12. Write a query to display the name (first_name, last_name) and salary for all
employees whose salary is not in the range $10,000 through $15,000.
Query-
mysql> select First_name, last_name, salary from Employees
-> where salary not between 10000 and 15000;
+------------+-----------+--------+
| First_name | last_name | salary |
+------------+-----------+--------+
| Steven | King | 2400 |
| Lex | De Haan | 17000 |
| Alexander | Hunold | 9000 |
| Bruce | Ernst | 6000 |
| David | Austin | 4800 |
| Valli | Pataballa | 4800 |
| Dinna | Lorentz | 4200 |
| Daniel | Faviet | 9000 |
| Neena | Kochhar | 18700 |
+------------+-----------+--------+
9 rows in set (0.00 sec)
___________________________________________________________________________________
___________________________________________________________________________________
___
___________________________________________________________________________________
___________________________________________________________________________________
___
Q13. Write a query to display the name (first_name, last_name) and department ID of
all employees in departments 30 or 100 in ascending order.
Query -
mysql> select First_name, last_name, DEPARTMENT_ID from Employees where
DEPARTMENT_ID in (30, 100) order by DEPARTMENT_ID ASC;
+------------+-----------+---------------+
| First_name | last_name | DEPARTMENT_ID |
+------------+-----------+---------------+
| Nancy | Greenberg | 100 |
| Daniel | Faviet | 100 |
+------------+-----------+---------------+
2 rows in set (0.00 sec)
___________________________________________________________________________________
___________________________________________________________________________________
__________
___________________________________________________________________________________
___________________________________________________________________________________
__________
Q14. Write a query to display the name (first_name, last_name) and salary for all
employees whose salary is not in the range $10,000 through $15,000 and are in
department 30 or 100.
Query -
mysql> select First_name, last_name, salary, DEPARTMENT_ID from Employees
-> where salary not between 10000 and 15000 and DEPARTMENT_ID in (30, 100);
+------------+-----------+--------+---------------+
| First_name | last_name | salary | DEPARTMENT_ID |
+------------+-----------+--------+---------------+
| Daniel | Faviet | 9000 | 100 |
+------------+-----------+--------+---------------+
1 row in set (0.00 sec)
___________________________________________________________________________________
___________________________________________________________________________________
____________
___________________________________________________________________________________
___________________________________________________________________________________
____________
Q15. Write a query to display the name (first_name, last_name) and hire date for
all employees who were hired in 1987.
Query -
mysql> select First_name, last_name, hire_date
-> from Employees
-> where year(hire_date) like '1987%';
+------------+-----------+------------+
| First_name | last_name | hire_date |
+------------+-----------+------------+
| Lex | De Haan | 1987-06-19 |
| Alexander | Hunold | 1987-06-20 |
| Bruce | Ernst | 1987-06-21 |
| David | Austin | 1987-06-22 |
| Valli | Pataballa | 1987-06-23 |
| Dinna | Lorentz | 1987-06-24 |
| Nancy | Greenberg | 1987-06-25 |
| Daniel | Faviet | 1987-06-26 |
+------------+-----------+------------+
8 rows in set (0.05 sec)
___________________________________________________________________________________
___________________________________________________________________________________
____________
___________________________________________________________________________________
___________________________________________________________________________________
____________
Q16. Write a query to display the first_name of all employees who have both "b" and
"c" in their first name.
Query -
mysql> select First_name from Employees
-> where First_name like '%b%' and First_name like '%c%';
+------------+
| First_name |
+------------+
| Bruce |
+------------+
1 row in set (0.00 sec)
___________________________________________________________________________________
___________________________________________________________________________________
____________
___________________________________________________________________________________
___________________________________________________________________________________
____________
Q17. Write a query to display the last name, job, and salary for all employees
whose job is that of a Programmer or a Shipping Clerk, and whose salary is not
equal to $4,500, $10,000, or $15,000.
Query -
mysql> select last_name, job_id, salary from Employees where job_id in ('IT_PROG',
'SH_CLERK') AND salary not in (4500, 10000, 15000);
+-----------+---------+--------+
| last_name | job_id | salary |
+-----------+---------+--------+
| Hunold | IT_PROG | 9000 |
| Ernst | IT_PROG | 6000 |
| Austin | IT_PROG | 4800 |
| Pataballa | IT_PROG | 4800 |
+-----------+---------+--------+
4 rows in set (0.02 sec)
___________________________________________________________________________________
___________________________________________________________________________________
____________
___________________________________________________________________________________
___________________________________________________________________________________
____________
Q18. Write a query to display the last name of employees whose names have exactly 6
characters.
Query -
mysql> select last_name from Employees where last_name like '______';
+-----------+
| last_name |
+-----------+
| Hunold |
| Austin |
| Faviet |
+-----------+
3 rows in set (0.03 sec)
___________________________________________________________________________________
___________________________________________________________________________________
____________
___________________________________________________________________________________
___________________________________________________________________________________
____________
Q19. Write a query to display the last name of employees having 'e' as the third
character.
Query -
mysql> select last_name from Employees where last_name like '___e%';
+-----------+
| last_name |
+-----------+
| Lorentz |
| Greenberg |
+-----------+
2 rows in set (0.04 sec)
___________________________________________________________________________________
___________________________________________________________________________________
____________
___________________________________________________________________________________
___________________________________________________________________________________
____________
Q20. Write a query to display the jobs/designations available in the employees
table.
Query -
mysql> select Distinct job_id from Employees;
+------------+
| job_id |
+------------+
| AD_PRES |
| AD_VP |
| IT_PROG |
| IT_POOG |
| FI_MGR |
| FI_ACCOUNT |
+------------+
6 rows in set (0.05 sec)
___________________________________________________________________________________
___________________________________________________________________________________
____________
___________________________________________________________________________________
___________________________________________________________________________________
____________
Q21. Write a query to select all record from employees where last name in King,
Ernst, Austin, Faviet.
Query
mysql> select * from Employees
-> where last_name in ('King', 'Ernst','Austin','Faviet');
+------+------------+-----------+---------+--------------+------------+------------
+--------+-----------+------------+---------------+
| eid | First_name | last_name | email | phone_no | job_id | hire_date
| salary | comission | MANAGER_ID | DEPARTMENT_ID |
+------+------------+-----------+---------+--------------+------------+------------
+--------+-----------+------------+---------------+
| 101 | Steven | King | SKING | 515.123.4567 | AD_PRES | 1998-06-17
| 2400 | 0 | 0 | 90 |
| 104 | Bruce | Ernst | BERNST | 590.423.4568 | IT_PROG | 1987-06-21
| 6000 | 0 | 103 | 60 |
| 105 | David | Austin | DAUSTIN | 590.423.4569 | IT_PROG | 1987-06-22
| 4800 | 0 | 103 | 60 |
| 109 | Daniel | Faviet | DFAVIET | 515.124.4169 | FI_ACCOUNT | 1987-06-26
| 9000 | 0 | 108 | 100 |
+------+------------+-----------+---------+--------------+------------+------------
+--------+-----------+------------+---------------+
4 rows in set (0.00 sec)