MCQ

MySQL Practice Exercises with Solutions – Part 2

We have split this collection of exercises on MYSQL into seven parts, this is the first one, then you will find the second part, the third part, the fourth part, the fifth part, the sixth part, seventh part, eighth part, and finally the ninth part these exercises are based on simple, advanced and finally complex MYSQL queries. These questions are based on the following two tables, Employee table and Reward table.
 

Employee table
+-------------+------------+-----------+----------+--------------+-------------+
| Employee_id | First_name | Last_name |  Salary  | Joining_date | Departement |
+-------------+------------+-----------+----------+--------------+-------------+
|     1       |     Bob    |   Kinto   | 1000000  |  2019-01-20  |   Finance   |
|     2       |    Jerry   |  Kansxo   | 6000000  |  2019-01-15  |     IT      |
|     3       |    Philip  |   Jose    | 8900000  |  2019-02-05  |   Banking   |
|     4       |    John    |  Abraham  | 2000000  |  2019-02-25  |  Insurance  |
|     5       |   Michael  |  Mathew   | 2200000  |  2019-02-28  |   Finance   |
|     6       |    Alex    |  chreketo | 4000000  |  2019-05-10  |     IT      |
|     7       |    Yohan   |   Soso    | 1230000  |  2019-06-20  |   Banking   |
+-------------+------------+-----------+----------+--------------+-------------+
Reward table
+-----------------+-------------+--------+
| Employee_ref_id | date_reward | amount |
+-----------------+-------------+--------+
|         1       | 2019-05-11  |  1000  | 
|         2       | 2019-02-15  |  5000  |
|         3       | 2019-04-22  |  2000  |
|         1       | 2019-06-20  |  8000  |
+-----------------+-------------+--------+
Download the file containing the SQL statements allowing the creation of these tables. (Click here)

 

 

Questions

1. Get the length of the text in the “First_name” column.

SELECT LENGTH(First_name) FROM employee;


MySQL String LengthMySQL String LengthIn SQL language the LENGTH() function allows to calculate the length of a string. Note: the function is based on the number of bytes. A…Read More

 

2. Get the employee’s first name after replacing ‘o’ with ‘#’.

SELECT REPLACE(First_name,'o','#') FROM employee;


MySQL REPLACEMySQL REPLACEREPLACE() function in MySQL replaces all occurrences of a substring in a string.   Example 1: In the example below, we replace “Java” with “MySQL”…Read More

 

3. Get the employee’s last name and first name in a single column separated by a ‘_’.

SELECT CONCAT(First_name,'_',Last_name) FROM employee;


MySQL CONCATMySQL CONCAT()The CONCAT() function in MySQL, allows you to concatenate two or more strings. The function allows one or more arguments, but its main use is…Read More

 

4. Get the year, month, and day from the “Joining_date” column.

SELECT YEAR(joining_date), MONTH(joining_date), DAY(joining_date) 
FROM employee;


How To Get Year From Date In MySQLHow To Get Year From Date In MySQLIn SQL language the YEAR() function allows extracting a year from a date in YYYY-MM-DD format.   Syntax: To use this function in a SQL…Read More How To Get Month From Date In MySQLHow To Get Month From Date In MySQLIn this tutorial, we are going to see how to get month from date in MySQL. In SQL language MONTH() function allows extracting month number…Read More

 

5. Get all employees in ascending order by first name.

SELECT * FROM employee ORDER BY First_name ASC;


MySQL ORDER BYMySQL ORDER BYThe ORDER BY keyword sorts the result set in ascending or descending order. To sort records in descending order, use DESC keyword. To sort in…Read More

 

 
6. Get all employees in descending order by first name.

SELECT * FROM employee ORDER BY First_name DESC;

 

7. Get all employees in ascending order by first name and descending order by salary.

SELECT * FROM employee ORDER BY First_name ASC, salary DESC;

 

8. Get employees whose first name is “Bob”.

SELECT * FROM employee WHERE First_name='Bob';

 

9. Get employees whose first name is “Bob” or “Alex”.

SELECT * FROM employee WHERE First_name IN ('Bob','Alex');


How to Check if Value Exists in a MySQL DatabaseHow to Check if Value Exists in a MySQL DatabaseIn this tutorial, we are going to see how to check if a value exists in a MySQL database. The IN operator allows you to…Read More

 

10. Get employees whose first name is neither “Bob” nor “Alex”.

SELECT * FROM employee WHERE First_name NOT IN ('Bob','Alex');


SQL IN OperatorSQL IN OperatorThe logical operator IN in SQL is used with the WHERE statement to check if a column is equal to one of the values included…Read More

 

11. What is SQL injection?

SQL injection is one of the techniques used by hackers to hack a website by injecting SQL commands into data fields.

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *