MySQL Assignment3
MySQL Assignment3
mysql>
2. Write a query to get unique department ID from employee table.
mysql> select *from employee2 order by 'First Name' desc;
+-------------+------------+-----------+---------------+--------------+------------+---------+----------+--
--------------+------------+---------------+
| EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAILID | PHONE_NUMBER |
HIRE_DATE | JOB_ID | SALARY | COMMISSION_PCT | MANAGER_ID |
DEPARTMENT_ID |
+-------------+------------+-----------+---------------+--------------+------------+---------+----------+--
--------------+------------+---------------+
| 100 | steven | King | NOT_AVALIBALE | 515.123.4567 | 1987-06-17 |
AD_PRES | 26400.00 | 0.10 | 200 | 10 |
| 101 | Neena | Kochhar | NOT_AVALIBALE | 515.123.4569 | 1987-06-18 |
AD_VP | 18700.00 | 0.10 | 200 | 10 |
| 102 | Lex | De Haan | NOT_AVALIBALE | 515.123.4569 | 1987-06-19 |
AD_VP | 18700.00 | 0.10 | 200 | 10 |
| 103 | Alexander | Hunold | NOT_AVALIBALE | 590.423.4567 | 1987-06-20 |
IT_PROG | 9900.00 | 0.10 | 130 | 60 |
| 104 | Bruce | Ernst | NOT_AVALIBALE | 590.423.4568 | 1987-06-21 |
IT_PROG | 6600.00 | 0.10 | 103 | 60 |
| 105 | David | Austin | NOT_AVALIBALE | 590.423.4569 | 1987-06-22 |
IT_PROG | 5280.00 | 0.10 | 103 | 60 |
| 106 | Valli | Pataballa | NOT_AVALIBALE | 590.423.456O | 1987-06-23 |
IT_PROG | 5280.00 | 0.10 | 103 | 60 |
| 107 | Diana | Lorentz | NOT_AVALIBALE | 590.423.5567 | 1987-06-24 |
IT_PROG | 4620.00 | 0.10 | 114 | 30 |
| 108 | Nancy | Greenberg | NOT_AVALIBALE | 515.124.4569 | 1987-06-25 |
SA_MAN | 13200.00 | 0.10 | 145 | 80 |
| 109 | Daniel | Faviet | NOT_AVALIBALE | 515.124.4169 | 1987-06-26 |
SA_MAN | 9900.00 | 0.10 | 145 | 80 |
+-------------+------------+-----------+---------------+--------------+------------+---------+----------+--
--------------+------------+---------------+
10 rows in set (0.00 sec)
3. Write a query to get all employee details from the employee table order by first
name, descending
mysql> select employee_id,first_name,last_name,salary from employee2;
+-------------+------------+-----------+----------+
| employee_id | first_name | last_name | salary |
+-------------+------------+-----------+----------+
| 100 | steven | King | 26400.00 |
| 101 | Neena | Kochhar | 18700.00 |
| 102 | Lex | De Haan | 18700.00 |
| 103 | Alexander | Hunold | 9900.00 |
| 104 | Bruce | Ernst | 6600.00 |
| 105 | David | Austin | 5280.00 |
| 106 | Valli | Pataballa | 5280.00 |
| 107 | Diana | Lorentz | 4620.00 |
| 108 | Nancy | Greenberg | 13200.00 |
| 109 | Daniel | Faviet | 9900.00 |
+-------------+------------+-----------+----------+
10 rows in set (0.04 sec)
4. Write a query to get the names (first_name, last_name), salary, PF of all the
employees (PF is calculated as 15% of salary).
select first_name,last_name,salary,salary * 0.15 AS PF
-> from employee2;
+------------+-----------+----------+-----------+
| first_name | last_name | salary | PF |
+------------+-----------+----------+-----------+
| steven | King | 26400.00 | 3960.0000 |
| Neena | Kochhar | 18700.00 | 2805.0000 |
| Lex | De Haan | 18700.00 | 2805.0000 |
| Alexander | Hunold | 9900.00 | 1485.0000 |
| Bruce | Ernst | 6600.00 | 990.0000 |
| David | Austin | 5280.00 | 792.0000 |
| Valli | Pataballa | 5280.00 | 792.0000 |
| Diana | Lorentz | 4620.00 | 693.0000 |
| Nancy | Greenberg | 13200.00 | 1980.0000 |
| Daniel | Faviet | 9900.00 | 1485.0000 |
+------------+-----------+----------+-----------+
10 rows in set (0.03 sec)
mysql>
5. Write a query to get the employee ID, names (first_name, last_name), salary in
ascending order of salary.
mysql> select employee_id,first_name,last_name,salary from employee2;
+-------------+------------+-----------+----------+
| employee_id | first_name | last_name | salary |
+-------------+------------+-----------+----------+
| 100 | steven | King | 26400.00 |
| 101 | Neena | Kochhar | 18700.00 |
| 102 | Lex | De Haan | 18700.00 |
| 103 | Alexander | Hunold | 9900.00 |
| 104 | Bruce | Ernst | 6600.00 |
| 105 | David | Austin | 5280.00 |
| 106 | Valli | Pataballa | 5280.00 |
| 107 | Diana | Lorentz | 4620.00 |
| 108 | Nancy | Greenberg | 13200.00 |
| 109 | Daniel | Faviet | 9900.00 |
+-------------+------------+-----------+----------+
10 rows in set (0.04 sec)
mysql>
6. Write a query to get the total salaries payable to employees.
mysql> SELECT SUM(SALARY)AS TOTAL_SALARY FROM EMPLOYEE2;
+--------------+
| TOTAL_SALARY |
+--------------+
| 118580.00 |
+--------------+
1 row in set (0.34 sec)
7. Write a query to get the maximum and minimum salary from employees table.
mysql> SELECT
-> MAX(salary) AS max_salary,
-> MIN(salary) AS min_salary
-> FROM employee2;
+------------+------------+
| max_salary | min_salary |
+------------+------------+
| 26400.00 | 4620.00 |
+------------+------------+
1 row in set (0.08 sec)
8. Write a query to get the average salary and number of employees in the employees
table.
mysql> SELECT COUNT(*)AS NO_OF_EMPL,AVG(SALARY)AS AVG_SALARY FROM
EMPLOYEE2;
+------------+--------------+
| NO_OF_EMPL | AVG_SALARY |
+------------+--------------+
| 10 | 11858.000000 |
+------------+--------------+
1 row in set (0.03 sec)
9. Write a query to get the number of employees working with the company.
mysql> SELECT COUNT(*) AS TOTAL_EMP FROM EMPLOYEE2;
+-----------+
| TOTAL_EMP |
+-----------+
| 10 |
+-----------+
1 row in set (0.77 sec)
10.Write a query to get the number of jobs available in the employees table
mysql> SELECT COUNT(JOB_ID) AS TOTAL_JOB_AVL FROM EMPLOYEE2;
+---------------+
| TOTAL_JOB_AVL |
+---------------+
| 10 |
+---------------+
1 row in set (0.00 sec)
11.Write a query to select first 10 records from a table.
mysql> SELECT *
-> FROM EMPLOYEE2 LIMIT 10;
+-------------+------------+-----------+---------------+--------------+------------+---------+----------+------------
----+------------+---------------+
| EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAILID | PHONE_NUMBER |
HIRE_DATE | JOB_ID | SALARY | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID |
+-------------+------------+-----------+---------------+--------------+------------+---------+----------+------------
----+------------+---------------+
| 100 | steven | King | NOT_AVALIBALE | 515.123.4567 | 1987-06-17 | AD_PRES |
26400.00 | 0.10 | 200 | 10 |
| 101 | Neena | Kochhar | NOT_AVALIBALE | 515.123.4569 | 1987-06-18 | AD_VP
| 18700.00 | 0.10 | 200 | 10 |
| 102 | Lex | De Haan | NOT_AVALIBALE | 515.123.4569 | 1987-06-19 | AD_VP |
18700.00 | 0.10 | 200 | 10 |
| 103 | Alexander | Hunold | NOT_AVALIBALE | 590.423.4567 | 1987-06-20 |
IT_PROG | 9900.00 | 0.10 | 130 | 60 |
| 104 | Bruce | Ernst | NOT_AVALIBALE | 590.423.4568 | 1987-06-21 | IT_PROG |
6600.00 | 0.10 | 103 | 60 |
| 105 | David | Austin | NOT_AVALIBALE | 590.423.4569 | 1987-06-22 | IT_PROG |
5280.00 | 0.10 | 103 | 60 |
| 106 | Valli | Pataballa | NOT_AVALIBALE | 590.423.456O | 1987-06-23 | IT_PROG |
5280.00 | 0.10 | 103 | 60 |
| 107 | Diana | Lorentz | NOT_AVALIBALE | 590.423.5567 | 1987-06-24 | IT_PROG |
4620.00 | 0.10 | 114 | 30 |
| 108 | Nancy | Greenberg | NOT_AVALIBALE | 515.124.4569 | 1987-06-25 |
SA_MAN | 13200.00 | 0.10 | 145 | 80 |
| 109 | Daniel | Faviet | NOT_AVALIBALE | 515.124.4169 | 1987-06-26 | SA_MAN |
9900.00 | 0.10 | 145 | 80 |
+-------------+------------+-----------+---------------+--------------+------------+---------+----------+------------
----+------------+---------------+
10 rows in set (0.00 sec)
mysql>
12.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
mysql> select first_name,last_name,salary from employee2 where salary<10000 or
salary>15000;
+------------+-----------+----------+
| first_name | last_name | salary |
+------------+-----------+----------+
| steven | King | 26400.00 |
| Neena | Kochhar | 18700.00 |
| Lex | De Haan | 18700.00 |
| Alexander | Hunold | 9900.00 |
| Bruce | Ernst | 6600.00 |
| David | Austin | 5280.00 |
| Valli | Pataballa | 5280.00 |
| Diana | Lorentz | 4620.00 |
| Daniel | Faviet | 9900.00 |
+------------+-----------+----------+
9 rows in set (0.00 sec)
13.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.
mysql> select department_id,first_name,last_name,salary from employee2 where
department_id in(30,100)order by first_name,last_name;
+---------------+------------+-----------+---------+
| department_id | first_name | last_name | salary |
+---------------+------------+-----------+---------+
| 30 | Diana | Lorentz | 4620.00 |
+---------------+------------+-----------+---------+
1 row in set (0.04 sec)
14.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.
mysql> select first_name,last_name,salary from employee2 where salary<10000 or
salary>15000 and department_id in (30,100);
+------------+-----------+---------+
| first_name | last_name | salary |
+------------+-----------+---------+
| Alexander | Hunold | 9900.00 |
| Bruce | Ernst | 6600.00 |
| David | Austin | 5280.00 |
| Valli | Pataballa | 5280.00 |
| Diana | Lorentz | 4620.00 |
| Daniel | Faviet | 9900.00 |
+------------+-----------+---------+
6 rows in set (0.00 sec)
15.Write a query to display the name (first_name, last_name) and hire date for all
employees who were hired in 1987.
mysql> select
-> first_name
-> ,last_name,
-> hire_date from employee2 where year (hire_date)=1987;
+------------+-----------+------------+
| first_name | last_name | hire_date |
+------------+-----------+------------+
| steven | King | 1987-06-17 |
| Neena | Kochhar | 1987-06-18 |
| 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 |
| Diana | Lorentz | 1987-06-24 |
| Nancy | Greenberg | 1987-06-25 |
| Daniel | Faviet | 1987-06-26 |
+------------+-----------+------------+
10 rows in set (0.03 sec)
mysql>
16.Write a query to display the first_name of all employees who have both "b" and "c" in
their first name
mysql> select first_name from employee2 where first_name like "%b%" and first_name
like "%c%";
+------------+
| first_name |
+------------+
| Bruce |
+------------+
1 row in set (0.14 sec)
17.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.
mysql> select last_name,job_id,salary from employee2 where
-> (job_id="IT_prog" or job_id="sa_man");
+-----------+---------+----------+
| last_name | job_id | salary |
+-----------+---------+----------+
| Hunold | IT_PROG | 9900.00 |
| Ernst | IT_PROG | 6600.00 |
| Austin | IT_PROG | 5280.00 |
| Pataballa | IT_PROG | 5280.00 |
| Lorentz | IT_PROG | 4620.00 |
| Greenberg | SA_MAN | 13200.00 |
| Faviet | SA_MAN | 9900.00 |
+-----------+---------+----------+
7 rows in set (0.00 sec)
18.Write a query to display the last name of employees whose names have exactly 6
characters.
mysql> SELECT LAST_NAME FROM EMPLOYEE2 WHERE LAST_NAME LIKE "______";
+-----------+
| LAST_NAME |
+-----------+
| Hunold |
| Austin |
| Faviet |
+-----------+
3 rows in set (0.00 sec)
19.Write a query to display the last name of employees having 'e' as the third character.
mysql> SELECT LAST_NAME FROM EMPLOYEE2 WHERE LAST_NAME LIKE "__E%";
+-----------+
| LAST_NAME |
+-----------+
| Greenberg |
+-----------+
1 row in set (0.00 sec)
20.Write a query to display the jobs/designations available in the employees table.
mysql> SELECT JOB_ID FROM EMPLOYEE2 GROUP BY JOB_ID;
+---------+
| JOB_ID |
+---------+
| AD_PRES |
| AD_VP |
| IT_PROG |
| SA_MAN |
+---------+
4 rows in set (0.10 sec)
21.Write a query to select all record from employees where last name in 'BLAKE', 'SCOTT',
'KING' and 'FORD’
mysql> SELECT * FROM EMPLOYEE2
-> WHERE LAST_NAME IN ('BLAKE', 'SCOTT', 'KING', 'FORD');
+-------------+------------+-----------+---------------+--------------+------------+---------+----------+------------
----+------------+---------------+
| EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAILID | PHONE_NUMBER |
HIRE_DATE | JOB_ID | SALARY | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID |
+-------------+------------+-----------+---------------+--------------+------------+---------+----------+------------
----+------------+---------------+
| 100 | steven | King | NOT_AVALIBALE | 515.123.4567 | 1987-06-17 | AD_PRES |
26400.00 | 0.10 | 200 | 10 |
+-------------+------------+-----------+---------------+--------------+------------+---------+----------+------------
----+------------+---------------+
1 row in set (0.00 sec)
mysql>