0% found this document useful (0 votes)
522 views45 pages

MySQL Basic SELECT Statement - Exercises, Practice, Solution

This document contains solutions to 19 exercises on writing basic SELECT statements in MySQL. The exercises include queries to retrieve employee names with aliases, unique department IDs, employee details ordered by first name descending, names, salary and PF, names and salary ordered by salary, total salaries, maximum and minimum salary, average salary and number of employees, number of employees, number of jobs, first names in upper case, first 3 characters of first names, calculation of 171*214+625, full names of employees, trimmed first names, length of names, and monthly salaries rounded to 2 decimal places. For each exercise, the relevant SQL query, relational algebra expression, and sample output are provided.

Uploaded by

Darwin Vargas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
522 views45 pages

MySQL Basic SELECT Statement - Exercises, Practice, Solution

This document contains solutions to 19 exercises on writing basic SELECT statements in MySQL. The exercises include queries to retrieve employee names with aliases, unique department IDs, employee details ordered by first name descending, names, salary and PF, names and salary ordered by salary, total salaries, maximum and minimum salary, average salary and number of employees, number of employees, number of jobs, first names in upper case, first 3 characters of first names, calculation of 171*214+625, full names of employees, trimmed first names, length of names, and monthly salaries rounded to 2 decimal places. For each exercise, the relevant SQL query, relational algebra expression, and sample output are provided.

Uploaded by

Darwin Vargas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 45

ACCENTURE BATCH 2 LABORATORY

MySQL basic SELECT statement - Exercises,


Practice, Solution
Page | 1 1. Write a query to display the names (first_name, last_name) using alias name

“First Name", "Last Name"

Sample table: employees

2. Write a query to get unique department ID from employee table.

Sample table: employees

3. Write a query to get all employee details from the employee table order by first
name, descending.

Sample table: employees

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).

Sample table: employees

5. Write a query to get the employee ID, names (first_name, last_name), salary
in ascending order of salary.

Sample table: employees

6. Write a query to get the total salaries payable to employees.

Sample table: employees


ACCENTURE BATCH 2 LABORATORY

7. Write a query to get the maximum and minimum salary from employees table.

Sample table: employees


Page | 2

8. Write a query to get the average salary and number of employees in the
employees table.

Sample table: employees

9. Write a query to get the number of employees working with the company.

Sample table: employees

10. Write a query to get the number of jobs available in the employees table

Sample table: employees

11. Write a query get all first name from employees table in upper case. 

Sample table: employees

12. Write a query to get the first 3 characters of first name from employees table. 

Sample table: employees

13. Write a query to calculate 171*214+625. 

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

Sample table: employees

15. Write a query to get first name from employees table after removing white
Page | 3
spaces from both side.

Sample table: employees

16. Write a query to get the length of the employee names (first_name,


last_name) from employees table. 

Sample table: employees

17. Write a query to check if the first_name fields of the employees table contains
numbers. 

Sample table: employees

18. Write a query to select first 10 records from a table. 

Sample table: employees

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.

Sample table: employees

Structure of 'hr' database:


ACCENTURE BATCH 2 LABORATORY

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".

Sample table: employees

Code:
SELECT first_name "First Name", last_name "Last Name"

FROM employees;

Copy

Pictorial Presentation of the above query

Result :
ACCENTURE BATCH 2 LABORATORY

Page | 6
ACCENTURE BATCH 2 LABORATORY

MySQL Basic Select Statement: Exercise-2 with Solution

Write a query to get unique department ID from employee table.


Page | 7
Sample table: employees

Code:
SELECT DISTINCT department_id

FROM employees;

Copy

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

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.

Sample table: employees

Code:
SELECT *

FROM employees

ORDER BY first_name DESC;

Copy

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 11

Result :
ACCENTURE BATCH 2 LABORATORY

Page | 12
ACCENTURE BATCH 2 LABORATORY

MySQL Basic Select Statement: Exercise-4 with Solution

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).

Sample table: employees

Code:
SELECT first_name, last_name, salary, salary*.15 PF

FROM employees;

Copy

Pictorial Presentation of the above query

Result :
ACCENTURE BATCH 2 LABORATORY

Page | 14
ACCENTURE BATCH 2 LABORATORY

MySQL Basic Select Statement: Exercise-5 with Solution

Write a query to get the employee ID, name (first_name, last_name), salary in
Page | 15 ascending order of salary.

Sample table: employees

Code:
SELECT employee_id, first_name, last_name, salary

FROM employees

ORDER BY salary;

Copy

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 16

Result :
ACCENTURE BATCH 2 LABORATORY

Page | 17
ACCENTURE BATCH 2 LABORATORY

MySQL Basic Select Statement: Exercise-6 with Solution

Write a query to get the total salaries payable to employees.


Page | 18
Sample table: employees

Code:
SELECT SUM(salary)

FROM employees;

Copy

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 19

Result :
ACCENTURE BATCH 2 LABORATORY

MySQL Basic Select Statement: Exercise-7 with Solution

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

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 21

Result :
ACCENTURE BATCH 2 LABORATORY

Page | 22
ACCENTURE BATCH 2 LABORATORY

MySQL Basic Select Statement: Exercise-8 with Solution

Write a query to get the average salary and number of employees in the
Page | 23 employees table.

Sample table: employees

Code:
SELECT AVG(salary), COUNT(*)

FROM employees;

Copy

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 24

Result :
ACCENTURE BATCH 2 LABORATORY

MySQL Basic Select Statement: Exercise-9 with Solution

Write a query to get the number of employees working with the company.
Page | 25
Sample table: employees

Code:
SELECT COUNT(*)

FROM employees;

Copy

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 26

Result :
ACCENTURE BATCH 2 LABORATORY

MySQL Basic Select Statement: Exercise-10 with Solution


Page | 27
Write a query to get the number of designations available in the employees table.

Sample table: employees

Code:
SELECT COUNT(DISTINCT job_id)

FROM employees;

Copy

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 28

Result :
ACCENTURE BATCH 2 LABORATORY

Page | 29
ACCENTURE BATCH 2 LABORATORY

MySQL Basic Select Statement: Exercise-11 with Solution

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

Pictorial Presentation of the above query

Result :
ACCENTURE BATCH 2 LABORATORY

Page | 31
ACCENTURE BATCH 2 LABORATORY

MySQL Basic Select Statement: Exercise-12 with Solution

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

Pictorial Presentation of the above query

Result :
ACCENTURE BATCH 2 LABORATORY

Page | 33
ACCENTURE BATCH 2 LABORATORY

MySQL Basic Select Statement: Exercise-13 with Solution

Write a query to calculate 171*214+625.


Page | 34
SELECT 171*214+625 Result;

Copy

Pictorial Presentation of the above query

Result :
ACCENTURE BATCH 2 LABORATORY

MySQL Basic Select Statement: Exercise-14 with Solution


Page | 35
Write a query to get the name (for example Ellen Abel, Sundar Ande etc.) of all
the employees from employees table.

Sample table: employees

Code:
SELECT CONCAT(first_name,' ', last_name) 'Employee Name'

FROM employees;

Copy

Pictorial Presentation of the above query

Result :
ACCENTURE BATCH 2 LABORATORY

Page | 36
ACCENTURE BATCH 2 LABORATORY

MySQL Basic Select Statement: Exercise-15 with Solution

Write a query to get first name of all employees table after removing white
Page | 37 spaces from both side.

Sample table: employees

Code:
SELECT TRIM(first_name)

FROM employees;

Copy

Pictorial Presentation of the above query

Result :
ACCENTURE BATCH 2 LABORATORY

Page | 38
ACCENTURE BATCH 2 LABORATORY

MySQL Basic Select Statement: Exercise-16 with Solution

Write a query to get the length of the employee names (first_name, last_name)
Page | 39 from employees table.

Sample table: employees

Code:
SELECT first_name,last_name, LENGTH(first_name)+LENGTH(last_name)
'Length of Names'

FROM employees;

Copy

Pictorial Presentation of the above query

Result :
ACCENTURE BATCH 2 LABORATORY

Page | 40
ACCENTURE BATCH 2 LABORATORY

MySQL Basic Select Statement: Exercise-17 with Solution

Write a query to check if the first_name fields of the employees table contains
Page | 41 numbers.

Sample table: employees

Code:
SELECT *

FROM employees

WHERE first_name REGEXP '[0-9]';


ACCENTURE BATCH 2 LABORATORY

MySQL Basic Select Statement: Exercise-18 with Solution

Write a query to select first 10 records from a table.


Page | 42
Sample table: employees

Code:
SELECT employee_id, first_name

FROM employees LIMIT 10;

Copy

Pictorial Presentation of the above query

Result :
ACCENTURE BATCH 2 LABORATORY

Page | 43
ACCENTURE BATCH 2 LABORATORY

MySQL Basic Select Statement: Exercise-19 with Solution

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

Pictorial Presentation of the above query

Result :
ACCENTURE BATCH 2 LABORATORY

Page | 45

You might also like