ACCENTURE BATCH 2 LABORATORY
MySQL Restricting and Sorting Data
1. Write a query to display the name (first_name, last_name) and salary for all
Page | 1
employees whose salary is not in the range $10,000 through $15,000.
Sample table: employees
2. Write a query to display the name (first_name, last_name) and department ID
of all employees in departments 30 or 100 in ascending order.
Sample table: employees
3. Write a query to display the name (first_name, last_name) and salary for all
employees whose salary is not in the range $10,000 through $15,000 and are in
department 30 or 100.
Sample table: employees
4. Write a query to display the name (first_name, last_name) and hire date for all
employees who were hired in 1987.
Sample table: employees
5. Write a query to display the first_name of all employees who have both "b" and
"c" in their first name.
Sample table: employees
6. Write a query to display the last name, job, and salary for all employees whose
job is that of a Programmer or a Shipping Clerk, and whose salary is not equal to
$4,500, $10,000, or $15,000.
Sample table: employees
7. Write a query to display the last name of employees whose names have
exactly 6 characters.
Sample table: employees
ACCENTURE BATCH 2 LABORATORY
8. Write a query to display the last name of employees having 'e' as the third
character.
Sample table: employees
Page | 2
9. Write a query to display the jobs/designations available in the employees
table.
Sample table: employees
10. Write a query to display the name (first_name, last_name), salary and PF
(15% of salary) of all employees.
Sample table: employees
11. Write a query to select all record from employees where last name in
'BLAKE', 'SCOTT', 'KING' and 'FORD'.
Sample table: employees
ACCENTURE BATCH 2 LABORATORY
Structure of 'hr' database:
Page | 3
ACCENTURE BATCH 2 LABORATORY
MySQL Restricting and Sorting Data: Exercise-1 with Solution
Write a query to display the names (first_name, last_name) and salary for all
Page | 4 employees whose salary is not in the range $10,000 through $15,000.
Sample table: employees
SELECT first_name, last_name, salary
FROM employees
WHERE salary NOT BETWEEN 10000 AND 15000;
Copy
Pictorial Presentation of the above query:
ACCENTURE BATCH 2 LABORATORY
Page | 5
ACCENTURE BATCH 2 LABORATORY
MySQL Restricting and Sorting Data: Exercise-2 with Solution
Write a query to display the name (first_name, last_name) and department ID of
Page | 6 all employees in departments 30 or 100 in ascending order.
Sample table: employees
Code:
SELECT first_name, last_name, department_id
FROM employees
WHERE department_id IN (30, 100)
ORDER BY department_id ASC;
Copy
Relational Algebra Expression:
Relational Algebra Tree:
ACCENTURE BATCH 2 LABORATORY
Page | 7
ACCENTURE BATCH 2 LABORATORY
Pictorial Presentation of the above query
Page | 8
ACCENTURE BATCH 2 LABORATORY
MySQL Restricting and Sorting Data: Exercise-3 with Solution
Page | 9
Write a query to display the name (first_name, last_name) and salary for all
employees whose salary is not in the range $10,000 through $15,000 and are in
department 30 or 100.
Sample table: employees
Code:
SELECT first_name, last_name, salary, department_id
FROM employees
WHERE salary NOT BETWEEN 10000 AND 15000
AND department_id IN (30, 100);
Copy
Relational Algebra Expression:
Relational Algebra Tree:
ACCENTURE BATCH 2 LABORATORY
Pictorial Presentation of the above query
Page | 10
ACCENTURE BATCH 2 LABORATORY
MySQL Restricting and Sorting Data: Exercise-4 with Solution
Write a query to display the name (first_name, last_name) and hire date for all
Page | 11 employees who were hired in 1987.
Sample table: employees
Code:
SELECT first_name, last_name, hire_date
FROM employees
WHERE YEAR(hire_date) LIKE '1987%';
Copy
ACCENTURE BATCH 2 LABORATORY
Pictorial Presentation of the above query
Page | 12
ACCENTURE BATCH 2 LABORATORY
MySQL Restricting and Sorting Data: Exercise-5 with Solution
Write a query to display the first_name of all employees who have both "b" and
Page | 13 "c" in their first name.
Sample table: employees
Code:
SELECT first_name
FROM employees
WHERE first_name LIKE '%b%'
AND first_name LIKE '%c%';
Copy
Relational Algebra Expression:
Relational Algebra Tree:
ACCENTURE BATCH 2 LABORATORY
Pictorial Presentation of the above query
Page | 14
ACCENTURE BATCH 2 LABORATORY
MySQL Restricting and Sorting Data: Exercise-6 with Solution
Write a query to display the last name, job, and salary for all employees whose
Page | 15 job is that of a Programmer or a Shipping Clerk, and salary is not equal to
$4,500, $10,000, or $15,000.
Sample table: employees
Code:
SELECT last_name, job_id, salary
FROM employees
WHERE job_id IN ('IT_PROG', 'SH_CLERK')
AND salary NOT IN (4500,10000, 15000);
Copy
Relational Algebra Expression:
Relational Algebra Tree:
ACCENTURE BATCH 2 LABORATORY
Pictorial Presentation of the above query
Page | 16
ACCENTURE BATCH 2 LABORATORY
MySQL Restricting and Sorting Data: Exercise-7 with Solution
Write a query to display the last name of employees whose name have exactly 6
Page | 17 characters.
Sample table: employees
Code:
SELECT last_name FROM employees WHERE last_name LIKE '______';
Copy
Relational Algebra Expression:
Relational Algebra Tree:
ACCENTURE BATCH 2 LABORATORY
Pictorial Presentation of the above query
Page | 18
ACCENTURE BATCH 2 LABORATORY
MySQL Restricting and Sorting Data: Exercise-8 with Solution
Write a query to display the last name of employees having 'e' as the third
Page | 19 character.
Sample table: employees
Code:
SELECT last_name FROM employees WHERE last_name LIKE '__e%';
Copy
Relational Algebra Expression:
Relational Algebra Tree:
ACCENTURE BATCH 2 LABORATORY
Pictorial Presentation of the above query
Page | 20
ACCENTURE BATCH 2 LABORATORY
MySQL Restricting and Sorting Data: Exercise-9 with Solution
Write a query to display the jobs/designations available in the employees table.
Page | 21
Sample table: employees
Code:
SELECT DISTINCT job_id FROM employees;
Copy
Relational Algebra Expression:
Relational Algebra Tree:
ACCENTURE BATCH 2 LABORATORY
Pictorial Presentation of the above query
Page | 22
ACCENTURE BATCH 2 LABORATORY
MySQL Restricting and Sorting Data: Exercise-10 with Solution
Write a query to display the name (first_name, last_name), salary and PF (15%
Page | 23 of salary) of all employees.
Sample table: employees
Code:
SELECT first_name, last_name, salary, salary*.15 PF from employees;
Copy
ACCENTURE BATCH 2 LABORATORY
Pictorial Presentation of the above query
Page | 24
ACCENTURE BATCH 2 LABORATORY
MySQL Restricting and Sorting Data: Exercise-11 with Solution
Write a query to select all records from employees where last name in 'JONES',
Page | 25 'BLAKE', 'SCOTT', 'KING' and 'FORD'.
Sample table: employees
Code:
SELECT *
FROM employees
WHERE last_name IN('JONES', 'BLAKE', 'SCOTT', 'KING', 'FORD');
Copy
Relational Algebra Expression:
Relational Algebra Tree:
ACCENTURE BATCH 2 LABORATORY
Pictorial Presentation of the above query
Page | 26