lab 3 answsers

advertisement
1. SHOW ALL EMPLOYEES AND THEIR TITLE WITH THE TITLE “WEB DESIGNER” OR
“SYSTEM ADMINISTRATOR” USING THE ‘IN’ FUNCTION
mysql> select firstname, lastname, title from employee_data where title
in ('Web Designer', 'System Administrator');
+-----------+----------+----------------------+
| firstname | lastname | title
|
+-----------+----------+----------------------+
| Anamika
| Pandit
| Web Designer
|
| Mary
| Anchor
| Web Designer
|
| Roger
| Lewis
| System Administrator |
| Danny
| Gibson
| System Administrator |
+-----------+----------+----------------------+
4 rows in set (0.00 sec)
2. SHOW EMPLOYEES AND THEIR TITLE WHOSE TITLE DOES NOT HAVE
“PROGRAMMER” OR “MARKETING EXECUTIVE” IN IT
mysql> select emp_id, firstname, lastname, title from Employee_data where
title not like "%Programmer%" and title not like "%Marketing Executive%";
+--------+-----------+----------+--------------------------+
| emp_id | firstname | lastname | title
|
+--------+-----------+----------+--------------------------+
|
1 | John
| Smith
| CEO
|
|
4 | Anamika
| Pandit
| Web Designer
|
|
5 | Mary
| Anchor
| Web Designer
|
|
13 | Kim
| Hunter
| Senior Web Designer
|
|
14 | Roger
| Lewis
| System Administrator
|
|
15 | Danny
| Gibson
| System Administrator
|
|
20 | Shahida
| Ali
| Customer Service Manager |
|
21 | Peter
| Champion | Finance Manager
|
|
22 | Harvey
| Rudoler | Financial Analyst
|
+--------+-----------+----------+--------------------------+
9 rows in set (0.00 sec)
3. SHOW ALL EMPLOYEES WITH THEIR AGE, WHO ARE BETWEEN 32 AND 40 (USE
THE BETWEEN FUNCTION)
mysql> select firstname, lastname, age from employee_data where age
between 32 and 40;
+-----------+------------+------+
| firstname | lastname
| age |
+-----------+------------+------+
| John
| Hagan
|
32 |
| Ganesh
| Pillai
|
32 |
| John
| MacFarland |
34 |
| Alok
| Nanda
|
32 |
| Hassan
| Rajabi
|
33 |
| Arthur
| Hoopla
|
32 |
| Kim
| Hunter
|
32 |
| Roger
| Lewis
|
35 |
| Danny
| Gibson
|
34 |
| Mike
| Harper
|
36 |
| Shahida
| Ali
|
32 |
| Peter
| Champion
|
36 |
+-----------+------------+------+
12 rows in set (0.00 sec)
4. SHOW ALL EMPLOYEES WITH SALARIES NOT BETWEEN 10000 AND 150000 (USE
THE BETWEEN FUNCTION)
mysql> select firstname, lastname, salary from employee_data where salary
not between 10000 and 150000;
+-----------+----------+--------+
| firstname | lastname | salary |
+-----------+----------+--------+
| John
| Smith
| 300000 |
+-----------+----------+--------+
1 row in set (0.00 sec)
5. SHOW ALL EMPLOYEES WITH SALARIES NOT BETWEEN 80000 AND 150000 (USE
THE BETWEEN FUNCTION)
mysql> select firstname, lastname, salary from employee_data where salary
not between 80000 and 150000;
+-----------+----------+--------+
| firstname | lastname | salary |
+-----------+----------+--------+
| John
| Smith
| 300000 |
| Fred
| Kruger
| 75000 |
| Edward
| Sakamuro | 75000 |
| Alok
| Nanda
| 70000 |
| Arthur
| Hoopla
| 75000 |
| Hal
| Simlai
| 70000 |
| Joseph
| Irvine
| 72000 |
| Shahida
| Ali
| 70000 |
+-----------+----------+--------+
8 rows in set (0.00 sec)
6. SHOW ALL EMPLOYEES IN ALPHABETICAL ORDER BY LAST NAME
mysql> select lastname, firstname from employee_data order by lastname;
+------------+-----------+
| lastname
| firstname |
+------------+-----------+
| Ali
| Shahida
|
| Anchor
| Mary
|
| Champion
| Peter
|
| Gibson
| Danny
|
| Hagan
| John
|
| Harper
| Mike
|
| Hoopla
| Arthur
|
| Hunter
| Kim
|
| Irvine
| Joseph
|
| Kruger
| Fred
|
| Lewis
| Roger
|
| MacFarland | John
|
| Nanda
| Alok
|
| Pandit
| Anamika
|
| Pillai
| Ganesh
|
| Rajabi
| Hassan
|
| Rudoler
| Harvey
|
| Sakamuro
| Edward
|
| Sehgal
| Monica
|
| Simlai
| Hal
|
| Simon
| Paul
|
| Smith
| John
|
+------------+-----------+
22 ows in set (0.03 sec)
7. SHOW ALL EMPLOYEES IN ALPHABETICAL ORDER BY SALARY
mysql> select firstname, lastname, salary from employee_data order by
salary, lastname;
+-----------+------------+-----------+
| firstname | lastname
| salary
|
+-----------+------------+-----------+
| Harvey
| Rudoler
| 52000.00 |
| Shahida
| Ali
| 70000.00 |
| Alok
| Nanda
| 70000.00 |
| Hal
| Simlai
| 70000.00 |
| Joseph
| Irvine
| 72000.00 |
| Arthur
| Hoopla
| 75000.00 |
| Fred
| Kruger
| 75000.00 |
| Edward
| Sakamuro
| 75000.00 |
| John
| MacFarland | 80000.00 |
| Mary
| Anchor
| 85000.00 |
| Paul
| Simon
| 85000.00 |
| Danny
| Gibson
| 90000.00 |
| Anamika
| Pandit
| 90000.00 |
| Hassan
| Rajabi
| 90000.00 |
| Monica
| Sehgal
| 90000.00 |
| Roger
| Lewis
| 100000.00 |
| Kim
| Hunter
| 110000.00 |
| Ganesh
| Pillai
| 110000.00 |
| Peter
| Champion
| 120000.00 |
| John
| Hagan
| 120000.00 |
| Mike
| Harper
| 120000.00 |
| John
| Smith
| 300000.00 |
+-----------+------------+-----------+
22 rows in set (0.03 sec)
8. SHOW ALL EMPLOYEE FIRSTNAMES IN DESCENDING ORDER
mysql> select firstname from employee_data order by firstname desc;
+-----------+
| firstname |
+-----------+
| Shahida
|
| Roger
|
| Peter
|
| Paul
|
| Monica
|
| Mike
|
| Mary
|
| Kim
|
| Joseph
|
| John
|
| John
|
| John
|
| Hassan
|
| Harvey
|
| Hal
|
| Ganesh
|
| Fred
|
| Edward
|
| Danny
|
| Arthur
|
| Anamika
|
| Alok
|
+-----------+
22 rows in set (0.00 sec)
9. SHOW FIRST 5 EMPLOYEES IN DATABASE
mysql> select firstname, lastname from employee_data limit 5;
+-----------+----------+
| firstname | lastname |
+-----------+----------+
| John
| Smith
|
| John
| Hagan
|
| Ganesh
| Pillai
|
| Anamika
| Pandit
|
| Mary
| Anchor
|
+-----------+----------+
5 rows in set (0.00 sec)
10.
SHOW EMPLOYEES BY AGE IN DESCENDING ORDER BUT LIMIT THE
NUMBER TO 5
mysql> select firstname, lastname, age from employee_data order by age
desc limit 5;
+-----------+----------+------+
| firstname | lastname | age |
+-----------+----------+------+
| Harvey
| Rudoler |
52 |
| John
| Smith
|
46 |
| Paul
| Simon
|
43 |
| Peter
| Champion |
36 |
| Mike
| Harper
|
36 |
+-----------+----------+------+
5 rows in set (0.00 sec)
11.
SHOW THE TWO YOUNGEST EMPLOYEES
mysql> select firstname, lastname age from employee_data order by age
limit 2;
+-----------+--------+
| firstname | age
|
+-----------+--------+
| Shahida
| Ali
|
| Mary
| Anchor |
+-----------+--------+
2 rows in set (0.00 sec)
12.
SHOW THE 3 EMPLOYEES, STARTING AT THE 6TH ROW OF DATA
mysql> select firstname, lastname from employee_data limit 6,3;
+-----------+------------+
| firstname | lastname
|
+-----------+------------+
| John
| MacFarland |
| Edward
| Sakamuro
|
| Alok
| Nanda
|
+-----------+------------+
3 rows in set (0.00 sec)
13.
SHOW ALL TITLES
mysql> select title from employee_data;
+----------------------------+
| title
|
+----------------------------+
| CEO
|
| Senior Programmer
|
| Senior Programmer
|
| Web Designer
|
| Web Designer
|
| Programmer
|
| Programmer
|
| Programmer
|
| Programmer
|
| Multimedia Programmer
|
| Multimedia Programmer
|
| Multimedia Programmer
|
| Senior Web Designer
|
| System Administrator
|
| System Administrator
|
| Senior Marketing Executive |
| Marketing Executive
|
| Marketing Executive
|
| Marketing Executive
|
| Customer Service Manager
|
| Finance Manager
|
| Financial Analyst
|
+----------------------------+
22 rows in set (0.00 sec)
14.
SHOW ALL TITLES, BUT ONLY SHOW EACH CATEGORY OF TITLE ONCE
(I.E., ONLY SHOW SENIOR PROGRAMMER ONCE)
mysql> select distinct title from employee_data;
+----------------------------+
| title
|
+----------------------------+
| CEO
|
| Senior Programmer
|
| Web Designer
|
| Programmer
|
| Multimedia Programmer
|
| Senior Web Designer
|
| System Administrator
|
| Senior Marketing Executive |
| Marketing Executive
|
| Customer Service Manager
|
| Finance Manager
|
| Financial Analyst
|
+----------------------------+
12 rows in set (0.00 sec)
15.
SHOW AGE OF EMPLOYEES BY AGE (DO NOT SHOW DUPLICATES)
mysql> select distinct age from employee_data order by age;
+------+
| age |
+------+
|
25 |
|
26 |
|
27 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
43 |
|
46 |
|
52 |
+------+
13 rows in set (0.00 sec)
16.
SHOW THE MINIMUM SALARY
mysql> select min(salary) from employee_data;
+-------------+
| min(salary) |
+-------------+
|
70000 |
+-------------+
1 row in set (0.02 sec)
17.
SHOW THE MAXIMUM SALARY
mysql> select max(salary) from employee_data;
+-------------+
| max(salary) |
+-------------+
|
300000 |
+-------------+
1 row in set (0.00 sec)
18.
SHOW THE TOTAL OF SALARIES PAID OUT TO EMPLOYEES
mysql> select sum(salary) from employee_data;
+-------------+
| sum(salary) |
+-------------+
|
2192000 |
+-------------+
1 row in set (0.01 sec)
19.
SHOW THE TOTAL OF PERKS PAID OUT TO EMPLOYEES
mysql> select sum(perks) from employee_data;
+------------+
| sum(perks) |
+------------+
|
386000 |
+------------+
1 row in set (0.00 sec)
20.
SHOW THE TOTAL OF SALARIES AND PERKS (HINT:
ADD SUMS)
mysql> select sum(salary) + sum(perks) from employee_data;
+--------------------------+
| sum(salary) + sum(perks) |
+--------------------------+
|
2578000 |
+--------------------------+
1 row in set (0.01 sec)
21.
SHOW THE AVERAGE AGE OF EMPLOYEES
mysql> select avg(age) from employee_data;
+----------+
| avg(age) |
+----------+
| 33.3636 |
+----------+
1 row in set (0.00 sec)
22.
SHOW THE AVERAGE SALARY
mysql> select avg(salary) from employee_data;
+-------------+
| avg(salary) |
+-------------+
| 99636.3636 |
+-------------+
1 row in set (0.00 sec)
23.
SHOW THE AVERAGE SALARY WITH THE COLUMN TITLE ‘AVERAGE
SALARY’
mysql> select avg(salary) as 'Average Salary' from employee_data;
+----------------+
| Average Salary |
+----------------+
|
99636.3636 |
+----------------+
1 row in set (0.02 sec)
24.
SHOW THE TOTAL NUMBER OF EMPLOYEES
mysql> select count(*) from employee_data;
+----------+
| count(*) |
+----------+
|
22 |
+----------+
1 row in set (0.00 sec)
25.
SHOW THE TOTAL NUMBER OF EMPLOYEES WITH THE TITLE
‘PROGRAMMER’
mysql> select count(*) from employee_data where title = 'Programmer';
+----------+
| count(*) |
+----------+
|
4 |
+----------+
1 row in set (0.00 sec)
26.
SHOW THE TITLES OF EMPLOYEES (DO NOT SHOW DUPLICATES, DO NOT
USE DISTINCT)
mysql> select title from employee_data group by title;
+----------------------------+
| title
|
+----------------------------+
| CEO
|
| Customer Service Manager
|
| Finance Manager
|
| Financial Analyst
|
| Marketing Executive
|
| Multimedia Programmer
|
| Programmer
|
| Senior Marketing Executive |
| Senior Programmer
|
| Senior Web Designer
|
| System Administrator
|
| Web Designer
|
+----------------------------+
12 rows in set (0.00 sec)
27.
SHOW THE COUNT OF EMPLOYEES WITH DIFFERENT TITLES (I.E.,
THERE ARE 4 PROGRAMMERS)
mysql> select title, count(*) from employee_data group by title;
+----------------------------+----------+
| title
| count(*) |
+----------------------------+----------+
| CEO
|
1 |
| Customer Service Manager
|
1 |
| Finance Manager
|
1 |
| Financial Analyst
|
1 |
| Marketing Executive
|
3 |
| Multimedia Programmer
|
3 |
| Programmer
|
4 |
| Senior Marketing Executive |
1 |
| Senior Programmer
|
2 |
| Senior Web Designer
|
1 |
| System Administrator
|
2 |
| Web Designer
|
2 |
+----------------------------+----------+
12 rows in set (0.00 sec)
28.
LIST THE NUMBER OF EMPLOYEES HOLDING DIFFERENT TITLES AND SORT
THEM
mysql> select title, count(*) as number from employee_data group by title
order by number;
+----------------------------+--------+
| title
| number |
+----------------------------+--------+
| Finance Manager
|
1 |
| Senior Web Designer
|
1 |
| Financial Analyst
|
1 |
| CEO
|
1 |
| Senior Marketing Executive |
1 |
| Customer Service Manager
|
1 |
| System Administrator
|
2 |
| Senior Programmer
|
2 |
| Web Designer
|
2 |
| Multimedia Programmer
|
3 |
| Marketing Executive
|
3 |
| Programmer
|
4 |
+----------------------------+--------+
12 rows in set (0.00 sec)
29.
LIST THE AVERAGE SALARY OF EMPLOYEES IN DIFFERENT DEPARTMENTS
(TITLES)
mysql> select title, avg(salary) from employee_data group by title;
+----------------------------+-------------+
| title
| avg(salary) |
+----------------------------+-------------+
| CEO
| 300000.0000 |
| Customer Service Manager
| 70000.0000 |
| Finance Manager
| 120000.0000 |
| Financial Analyst
| 95000.0000 |
| Marketing Executive
| 77333.3333 |
| Multimedia Programmer
| 83333.3333 |
| Programmer
| 75000.0000 |
| Senior Marketing Executive | 120000.0000 |
| Senior Programmer
| 115000.0000 |
| Senior Web Designer
| 110000.0000 |
| System Administrator
| 95000.0000 |
| Web Designer
| 87500.0000 |
+----------------------------+-------------+
12 rows in set (0.00 sec)
30.
LIST ONLY THE DEPARTMENTS WHERE THE AVERAGE SALARY IS MORE THAN
$100000
mysql> select title, avg(salary) from employee_data group by title having
avg(salary) > 100000;
+----------------------------+-------------+
| title
| avg(salary) |
+----------------------------+-------------+
| CEO
| 300000.0000 |
| Finance Manager
| 120000.0000 |
| Senior Marketing Executive | 120000.0000 |
| Senior Programmer
| 115000.0000 |
| Senior Web Designer
| 110000.0000 |
+----------------------------+-------------+
5 rows in set (0.00 sec)
31.
INCREASE THE CEO’S SALARY BY $20000 AND PERKS BY $5000. HIS
PREVIOUS SALARY WAS $200000 AND PERKS WERE $50000.
mysql> update employee_data set salary = salary + 20000, perks = perks +
5000 where title = 'CEO';
Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0
32.
SHOW SALARY AND PERKS FOR CEO
mysql> select salary, perks from employee_data where title = 'CEO';
+--------+-------+
| salary | perks |
+--------+-------+
| 320000 | 30000 |
+--------+-------+
1 row in set (0.00 sec)
33.
CHANGE THE TITLE OF WEB DESIGNER TO WEB DEVELOPER
mysql> update employee_data set title= 'Web Developer' where title = 'Web
Designer';
Query OK, 2 rows affected (0.02 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> \t
Download