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

Activity Exercise Assignment Week 10 CC105

The document provides 20 SQL problems or queries to practice writing SQL statements. The problems cover topics like selecting data, filtering results, aggregating data, and joining tables.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Activity Exercise Assignment Week 10 CC105

The document provides 20 SQL problems or queries to practice writing SQL statements. The problems cover topics like selecting data, filtering results, aggregating data, and joining tables.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Week 10 Part 2

Problems:

1. Write an SQL query to print all bonus details from the bonus table order by bonus_amount Ascending.

2. Write an sql query to fetch unique values of bonus amount from bonus table
3. Write an SQL query to print all Worker details from the Worker table order by LAST_NAME Ascending

4. Write an SQL query to print details of the Workers whose SALARY lies between 100000 and 500000

5. Write an SQL query to fetch the names of workers who earn the highest salary.
6. Prints worker details using their joining date

7. Count Workers that works in admin dept

8. Write an SQL query to show all departments along with the number of people in there

9. Write an SQL query to show the second highest salary from a table.
10. Write an SQL query to print details of Workers with DEPARTMENT name as “Executive”.

11. Write an SQL query to fetch the count of employees working in the department ‘Executive’.

12. Write an SQL query to print the first three characters of FIRST_NAME from Worker table.
13. Write an SQL query to print the FIRST_NAME from Worker table after replacing ‘a’ with ‘A’.

14. Write an SQL query that will print all employees from the HR department ordered by first name.

15. Write an SQL query that will print all employees from the HR department ordered by last name
16. Write an SQL query that will print all employees from the HR department ordered by salary.

17. Write an SQL query that will print all bonus between 3000 and 6000

18. Write an SQL query that will print all employees with an 'M' anywhere in their first name
19. Write an SQL query that will print all unique employee titles.

20. Write a query that counts the number of workers from the worker id

Full CMD config

Microsoft Windows [Version 10.0.19043.1237]

(c) Microsoft Corporation. All rights reserved.

C:\Users\larze>D:

D:\>cd Xammp

D:\Xammp>cd mysql\bin
D:\Xammp\mysql\bin>mysql -u root

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MariaDB connection id is 37

Server version: 10.4.21-MariaDB mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database employees;

Query OK, 1 row affected (0.001 sec)

MariaDB [(none)]> show databases;

+--------------------+

| Database |

+--------------------+

| cc105_lab_acts |

| employees |

| information_schema |

| logistics |

| mysql |

| performance_schema |

| phpmyadmin |

| test |

+--------------------+

8 rows in set (0.001 sec)

MariaDB [(none)]> use employees;

Database changed
MariaDB [employees]> create table worker(worker_id int(5) PRIMARY KEY, first_name varchar(20),
last_name varchar(20), salary int(15), joining_date datetime, department varchar(10);

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
MariaDB server version for the right syntax to use near '' at line 1

MariaDB [employees]> create table worker(worker_id int(5) PRIMARY KEY, first_name varchar(20),
last_name varchar(20), salary int(15), joining_date datetime, department varchar(10));

Query OK, 0 rows affected (0.990 sec)

MariaDB [employees]> create table bonus(worker_ref_id int(5) PRIMARY KEY, bonus_date datetime,
bonus_amount int(10));

Query OK, 0 rows affected (0.257 sec)

MariaDB [employees]> create table title(worker_ref_id int(5) PRIMARY KEY, worker_title varchar(15),
affected_from varchar(15));

Query OK, 0 rows affected (0.542 sec)

MariaDB [employees]> show tables;

+---------------------+

| Tables_in_employees |

+---------------------+

| bonus |

| title |

| worker |

+---------------------+

3 rows in set (0.058 sec)

MariaDB [employees]> INSERT INTO bonus VALUES(1, '20/02/2016 0:00', '5000'), (2, '11/06/2016 0:00',
'3000'),(3, '20/02/2016 0:00', '4000'), (1, '20/06/2016 0:00', '4500'), (2, '11/12/2016 0:00', '3500');

ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

MariaDB [employees]> create table bonus(worker_ref_id int(5) , bonus_date datetime, bonus_amount


int(10);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
MariaDB server version for the right syntax to use near '' at line 1

MariaDB [employees]> create table bonus(worker_ref_id int(5), bonus_date datetime, bonus_amount


int(10);

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
MariaDB server version for the right syntax to use near '' at line 1

MariaDB [employees]> create table bonus(worker_ref_id int(5), bonus_date datetime, bonus_amount


int(10));

Query OK, 0 rows affected (1.990 sec)

MariaDB [employees]> show tables;

+---------------------+

| Tables_in_employees |

+---------------------+

| bonus |

| title |

| worker |

+---------------------+

3 rows in set (0.002 sec)

MariaDB [employees]> INSERT INTO worker VALUES

-> (1, 'Ryan', 'Barbacena', 100000, 20/02/2014 9:00, 'HR'),

-> (2, 'Allea Marie', 'Royo', 80000, 11/06/2014 9:00, 'Admin'),

-> (3, 'John Jerty', 'Pasahol', 300000, 20/02/2014 9:00, 'HR'),

-> (4, 'Brigee Rafael', 'Tobes', 500000, 20/02/2014 9:00, 'Admin'),

-> (5, 'Kelly', 'Mercado', 500000, 11/06/2014 9:00, 'Admin'),

-> (6, 'Krizele Marielle', 'Aniel', 200000, 11/06/2014 9:00, 'Account'),

-> (7, 'Ryan', 'Barbacena', 75000, 20/01/2014 9:00, 'Account'),

-> (8, 'Mae', 'Abo', 90000, 11/04/2014 9:00, 'Admin');


ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
MariaDB server version for the right syntax to use near '9:00, 'HR'),

(2, 'Allea Marie', 'Royo', 80000, 11/06/2014 9:00, 'Admin'),

(3,...' at line 2

MariaDB [employees]> INSERT INTO worker VALUES(1, 'Ryan', 'Barbacena', 100000, 20/02/2014 9:00,
'HR'),(2, 'Allea Marie', 'Royo', 80000, 11/06/2014 9:00, 'Admin'),(3, 'John Jerty', 'Pasahol', 300000,
20/02/2014 9:00, 'HR'),(4, 'Brigee Rafael', 'Tobes', 500000, 20/02/2014 9:00, 'Admin'),(5, 'Kelly',
'Mercado', 500000, 11/06/2014 9:00, 'Admin'),(6, 'Krizele Marielle', 'Aniel', 200000, 11/06/2014 9:00,
'Account'),(7, 'Johara', 'Veniegas', 75000, 20/01/2014 9:00, 'Account'),(8, 'Mae', 'Abo', 90000,
11/04/2014 9:00, 'Admin');

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
MariaDB server version for the right syntax to use near '9:00, 'HR'),(2, 'Allea Marie', 'Royo', 80000,
11/06/2014 9:00, 'Admin'),(3, '...' at line 1

MariaDB [employees]> INSERT INTO worker VALUES(1, 'Ryan', 'Barbacena', 100000, '20/02/2014 9:00',
'HR'),(2, 'Allea Marie', 'Royo', 80000, '11/06/2014 9:00', 'Admin'),(3, 'John Jerty', 'Pasahol', 300000,
'20/02/2014 9:00', 'HR'),(4, 'Brigee Rafael', 'Tobes', 500000, '20/02/2014 9:00', 'Admin'),(5, 'Kelly',
'Mercado', 500000, '11/06/2014 9:00', 'Admin'),(6, 'Krizele Marielle', 'Aniel', 200000, '11/06/2014 9:00',
'Account'),(7, 'Johara', 'Veniegas', 75000, '20/01/2014 9:00', 'Account'),(8, 'Mae', 'Abo', 90000,
'11/04/2014 9:00', 'Admin');

Query OK, 8 rows affected, 8 warnings (0.071 sec)

Records: 8 Duplicates: 0 Warnings: 8

MariaDB [employees]> SELECT * FROM employees;

ERROR 1146 (42S02): Table 'employees.employees' doesn't exist

MariaDB [employees]> SELECT * FROM worker;

+-----------+------------------+-----------+--------+---------------------+------------+

| worker_id | first_name | last_name | salary | joining_date | department |

+-----------+------------------+-----------+--------+---------------------+------------+

| 1 | Ryan | Barbacena | 100000 | 0000-00-00 00:00:00 | HR |

| 2 | Allea Marie | Royo | 80000 | 0000-00-00 00:00:00 | Admin |

| 3 | John Jerty | Pasahol | 300000 | 0000-00-00 00:00:00 | HR |

| 4 | Brigee Rafael | Tobes | 500000 | 0000-00-00 00:00:00 | Admin |

| 5 | Kelly | Mercado | 500000 | 0000-00-00 00:00:00 | Admin |


| 6 | Krizele Marielle | Aniel | 200000 | 0000-00-00 00:00:00 | Account |

| 7 | Johara | Veniegas | 75000 | 0000-00-00 00:00:00 | Account |

| 8 | Mae | Abo | 90000 | 0000-00-00 00:00:00 | Admin |

+-----------+------------------+-----------+--------+---------------------+------------+

8 rows in set (0.002 sec)

MariaDB [employees]> delete from worker;

Query OK, 8 rows affected (1.751 sec)

MariaDB [employees]> select * from worker;

Empty set (0.001 sec)

MariaDB [employees]> INSERT INTO worker VALUES(1, 'Ryan', 'Barbacena', 100000, '20-02-2014 9:00',
'HR'),(2, 'Allea Marie', 'Royo', 80000, '11-06-2014 9:00', 'Admin'),(3, 'John Jerty', 'Pasahol', 300000, '20-
02-2014 9:00', 'HR'),(4, 'Brigee Rafael', 'Tobes', 500000, '20-02-2014 9:00', 'Admin'),(5, 'Kelly', 'Mercado',
500000, '11-06-2014 9:00', 'Admin'),(6, 'Krizele Marielle', 'Aniel', 200000, '11-06-2014 9:00', 'Account'),
(7, 'Johara', 'Veniegas', 75000, '20-01-2014 9:00', 'Account'),(8, 'Mae', 'Abo', 90000, '11-04-2014 9:00',
'Admin');

Query OK, 8 rows affected, 8 warnings (0.412 sec)

Records: 8 Duplicates: 0 Warnings: 8

MariaDB [employees]> select * from worker;

+-----------+------------------+-----------+--------+---------------------+------------+

| worker_id | first_name | last_name | salary | joining_date | department |

+-----------+------------------+-----------+--------+---------------------+------------+

| 1 | Ryan | Barbacena | 100000 | 0000-00-00 00:00:00 | HR |

| 2 | Allea Marie | Royo | 80000 | 0000-00-00 00:00:00 | Admin |

| 3 | John Jerty | Pasahol | 300000 | 0000-00-00 00:00:00 | HR |

| 4 | Brigee Rafael | Tobes | 500000 | 0000-00-00 00:00:00 | Admin |

| 5 | Kelly | Mercado | 500000 | 0000-00-00 00:00:00 | Admin |


| 6 | Krizele Marielle | Aniel | 200000 | 0000-00-00 00:00:00 | Account |

| 7 | Johara | Veniegas | 75000 | 0000-00-00 00:00:00 | Account |

| 8 | Mae | Abo | 90000 | 0000-00-00 00:00:00 | Admin |

+-----------+------------------+-----------+--------+---------------------+------------+

8 rows in set (0.001 sec)

MariaDB [employees]> delete from worker;

Query OK, 8 rows affected (0.070 sec)

MariaDB [employees]> INSERT INTO worker VALUES(1, 'Ryan', 'Barbacena', 100000, '2014-02-20 09:00',
'HR'),(2, 'Allea Marie', 'Royo', 80000, '2014-06-11 09:00', 'Admin'),(3, 'John Jerty', 'Pasahol', 300000,
'2014-02-20 09:00', 'HR'),(4, 'Brigee Rafael', 'Tobes', 500000, '2014-02-20 09:00', 'Admin'),(5, 'Kelly',
'Mercado', 500000, '2014-06-11 09:00', 'Admin'),(6, 'Krizele Marielle', 'Aniel', 200000, '2014-06-20
09:00', 'Account'),(7, 'Johara', 'Veniegas', 75000, '2014-01-20 09:00', 'Account'),(8, 'Mae', 'Abo', 90000,
'2014-04-11 09:00', 'Admin');

Query OK, 8 rows affected (1.809 sec)

Records: 8 Duplicates: 0 Warnings: 0

MariaDB [employees]> select * from worker;

+-----------+------------------+-----------+--------+---------------------+------------+

| worker_id | first_name | last_name | salary | joining_date | department |

+-----------+------------------+-----------+--------+---------------------+------------+

| 1 | Ryan | Barbacena | 100000 | 2014-02-20 09:00:00 | HR |

| 2 | Allea Marie | Royo | 80000 | 2014-06-11 09:00:00 | Admin |

| 3 | John Jerty | Pasahol | 300000 | 2014-02-20 09:00:00 | HR |

| 4 | Brigee Rafael | Tobes | 500000 | 2014-02-20 09:00:00 | Admin |

| 5 | Kelly | Mercado | 500000 | 2014-06-11 09:00:00 | Admin |

| 6 | Krizele Marielle | Aniel | 200000 | 2014-06-20 09:00:00 | Account |

| 7 | Johara | Veniegas | 75000 | 2014-01-20 09:00:00 | Account |

| 8 | Mae | Abo | 90000 | 2014-04-11 09:00:00 | Admin |


+-----------+------------------+-----------+--------+---------------------+------------+

8 rows in set (0.001 sec)

MariaDB [employees]> INSERT INTO bonus VALUES(1, '2016-02-20 00:00', '5000'),(2, '2016-06-11 00:00',
'3000'),(3, '2016-02-20 00:00', '4000'),(1, '2016-06-20 00:00', '4500'),(2, '2016-12-11 00:00', '3500');

Query OK, 5 rows affected (1.812 sec)

Records: 5 Duplicates: 0 Warnings: 0

MariaDB [employees]> select * from bonus;

+---------------+---------------------+--------------+

| worker_ref_id | bonus_date | bonus_amount |

+---------------+---------------------+--------------+

| 1 | 2016-02-20 00:00:00 | 5000 |

| 2 | 2016-06-11 00:00:00 | 3000 |

| 3 | 2016-02-20 00:00:00 | 4000 |

| 1 | 2016-06-20 00:00:00 | 4500 |

| 2 | 2016-12-11 00:00:00 | 3500 |

+---------------+---------------------+--------------+

5 rows in set (0.000 sec)

MariaDB [employees]> INSERT INTO title VALUES(1, 'Manager', '2016-02-20 00:00'),(2, 'Executive',
'2016-06-11 00:00'),(8, 'Executive', '2016-06-11 00:00'),(5, 'Manager', '2016-06-11 00:00'),(4, 'Asst.
Manager', '2016-06-11 00:00'),(7, 'Executive', '2016-06-11 00:00'),(6, 'Lead', '2016-06-11 00:00'),(3,
'Lead', '2016-06-11 00:00');

Query OK, 8 rows affected, 8 warnings (1.767 sec)

Records: 8 Duplicates: 0 Warnings: 8

MariaDB [employees]> select * from title;

+---------------+---------------+-----------------+

| worker_ref_id | worker_title | affected_from |


+---------------+---------------+-----------------+

| 1 | Manager | 2016-02-20 00:0 |

| 2 | Executive | 2016-06-11 00:0 |

| 3 | Lead | 2016-06-11 00:0 |

| 4 | Asst. Manager | 2016-06-11 00:0 |

| 5 | Manager | 2016-06-11 00:0 |

| 6 | Lead | 2016-06-11 00:0 |

| 7 | Executive | 2016-06-11 00:0 |

| 8 | Executive | 2016-06-11 00:0 |

+---------------+---------------+-----------------+

8 rows in set (0.007 sec)

MariaDB [employees]>

You might also like