MySQL Basic SELECT Statement - Exercises, Practice, Solution
MySQL Basic SELECT Statement - Exercises, Practice, Solution
3. Write a query to get all employee details from the employee table order by first
name, descending.
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).
5. Write a query to get the employee ID, names (first_name, last_name), salary
in ascending order of salary.
7. Write a query to get the maximum and minimum salary from employees table.
8. Write a query to get the average salary and number of employees in the
employees table.
9. Write a query to get the number of employees working with the company.
10. Write a query to get the number of jobs available in the employees table
11. Write a query get all first name from employees table in upper case.
12. Write a query to get the first 3 characters of first name from employees table.
14. Write a query to get the names (for example Ellen Abel, Sundar Ande etc.) of
all the employees from employees table.
ACCENTURE BATCH 2 LABORATORY
15. Write a query to get first name from employees table after removing white
Page | 3
spaces from both side.
17. Write a query to check if the first_name fields of the employees table contains
numbers.
19. Write a query to get monthly salary (round 2 decimal places) of each and
every employee
Note : Assume the salary field provides the 'annual salary' information.
Page | 4
ACCENTURE BATCH 2 LABORATORY
SOLUTION
Page | 5
MySQL Basic Select Statement: Exercise-1 with Solution
Write a query to display the names (first_name, last_name) using alias name
"First Name", "Last Name".
Code:
SELECT first_name "First Name", last_name "Last Name"
FROM employees;
Copy
Result :
ACCENTURE BATCH 2 LABORATORY
Page | 6
ACCENTURE BATCH 2 LABORATORY
Code:
SELECT DISTINCT department_id
FROM employees;
Copy
Page | 8
Result :
ACCENTURE BATCH 2 LABORATORY
Page | 9
ACCENTURE BATCH 2 LABORATORY
Page | 10
MySQL Basic Select Statement: Exercise-3 with Solution
Write a query to get the details of all employees according to first name in
descending order.
Code:
SELECT *
FROM employees
Copy
Page | 11
Result :
ACCENTURE BATCH 2 LABORATORY
Page | 12
ACCENTURE BATCH 2 LABORATORY
Write a query to get the names (first_name, last_name), salary, PF of all the
Page | 13 employees (PF is calculated as 15% of salary).
Code:
SELECT first_name, last_name, salary, salary*.15 PF
FROM employees;
Copy
Result :
ACCENTURE BATCH 2 LABORATORY
Page | 14
ACCENTURE BATCH 2 LABORATORY
Write a query to get the employee ID, name (first_name, last_name), salary in
Page | 15 ascending order of salary.
Code:
SELECT employee_id, first_name, last_name, salary
FROM employees
ORDER BY salary;
Copy
Page | 16
Result :
ACCENTURE BATCH 2 LABORATORY
Page | 17
ACCENTURE BATCH 2 LABORATORY
Code:
SELECT SUM(salary)
FROM employees;
Copy
Page | 19
Result :
ACCENTURE BATCH 2 LABORATORY
Write a query to get the maximum and minimum salary from employees table.
Page | 20
Sample table: employees
Code:
SELECT MAX(salary), MIN(salary)
FROM employees;
Copy
Page | 21
Result :
ACCENTURE BATCH 2 LABORATORY
Page | 22
ACCENTURE BATCH 2 LABORATORY
Write a query to get the average salary and number of employees in the
Page | 23 employees table.
Code:
SELECT AVG(salary), COUNT(*)
FROM employees;
Copy
Page | 24
Result :
ACCENTURE BATCH 2 LABORATORY
Write a query to get the number of employees working with the company.
Page | 25
Sample table: employees
Code:
SELECT COUNT(*)
FROM employees;
Copy
Page | 26
Result :
ACCENTURE BATCH 2 LABORATORY
Code:
SELECT COUNT(DISTINCT job_id)
FROM employees;
Copy
Page | 28
Result :
ACCENTURE BATCH 2 LABORATORY
Page | 29
ACCENTURE BATCH 2 LABORATORY
Write a query get all first name from employees table in upper case.
Page | 30
Sample table: employees
Code:
SELECT UPPER(first_name)
FROM employees;
Copy
Result :
ACCENTURE BATCH 2 LABORATORY
Page | 31
ACCENTURE BATCH 2 LABORATORY
Write a query to get the first three characters of first name of all employees.
Page | 32
Sample table: employees
Code:
SELECT SUBSTRING(first_name,1,3)
FROM employees;
Copy
Result :
ACCENTURE BATCH 2 LABORATORY
Page | 33
ACCENTURE BATCH 2 LABORATORY
Copy
Result :
ACCENTURE BATCH 2 LABORATORY
Code:
SELECT CONCAT(first_name,' ', last_name) 'Employee Name'
FROM employees;
Copy
Result :
ACCENTURE BATCH 2 LABORATORY
Page | 36
ACCENTURE BATCH 2 LABORATORY
Write a query to get first name of all employees table after removing white
Page | 37 spaces from both side.
Code:
SELECT TRIM(first_name)
FROM employees;
Copy
Result :
ACCENTURE BATCH 2 LABORATORY
Page | 38
ACCENTURE BATCH 2 LABORATORY
Write a query to get the length of the employee names (first_name, last_name)
Page | 39 from employees table.
Code:
SELECT first_name,last_name, LENGTH(first_name)+LENGTH(last_name)
'Length of Names'
FROM employees;
Copy
Result :
ACCENTURE BATCH 2 LABORATORY
Page | 40
ACCENTURE BATCH 2 LABORATORY
Write a query to check if the first_name fields of the employees table contains
Page | 41 numbers.
Code:
SELECT *
FROM employees
Code:
SELECT employee_id, first_name
Copy
Result :
ACCENTURE BATCH 2 LABORATORY
Page | 43
ACCENTURE BATCH 2 LABORATORY
Write a query to get monthly salary (round 2 decimal places) of all employees.
Page | 44
Sample table: employees
Code:
SELECT first_name, last_name, round(salary/12,2) as 'Monthly Salary'
FROM employees;
Copy
Result :
ACCENTURE BATCH 2 LABORATORY
Page | 45