Practice Day1
Practice Day1
Login Info
The user /password we are going to use is :
“root” and it was granted administrator privileges
“statistica” to log on
Used Tools
MySQL 8.0
MySQL Workbench 8.0
Notepad++
Adobe Acrobat Reader
Exercises
Exercise 1 – Restricting and Sorting Data
Exercise 2 – Single‐Row Functions to Customize Output
Exercise 3 – Conversion Functions and Conditional Expressions
Exercise 4 – Using the Group Functions
Exercise 5 – Using Joins
Exercise 6 – Using Subqueries
Exercise 7 – Retrieve Data Using Subqueries
Test problems – 17, 24, 25, 26, 27, 29, 30
Exercise 8 – Set Operators
Exercise 9 – Using DML Statements
Exercise 10 – Manipulate Data
Exercise 11 – Data Definition Language
Exercise 12 – Constraints
Exercise 13 – Other Schema Objects
Test problems – 12, 16, 18, 19, 23, 28
Basic SQL
• Selecting data and changing the order of the rows that are displayed
• Restricting rows by using the WHERE clause
• Sorting rows by using the ORDER BY clause
• Using substitution variables to add flexibility to your SQL SELECT statements
1. Because of budget issues, the HR department needs a report that displays the last name
and salary of employees who earn more than $12,000. Save your SQL statement as a file
named lab_01_01.sql. Run your query.
2. Create a report that displays the last name and department number for employee
number 176.
3. Modify lab_01_01.sql to display the last name and salary for any employee whose salary
is not in the range $5,000 through $12,000. Save your SQL statement as lab_01_03.sql.
4. Create a report to display the last name, job ID, and hire date for employees with the last
names of Matos and Taylor. Order the query in ascending order by hire date.
6. Modify lab_01_03.sql to display the last name and salary of employees who earn
between $5,000 and $12,000, and are in department 20 or 50. Label the columns Employee
and Monthly Salary, respectively. Save lab_01_03.sql as lab_01_06.sql again. Save the
statement in lab_01_06.sql.
7. The HR department needs a report that displays the last name and hire date of all
employees who were hired in 2006.
8. Create a report to display the last name and job ID of all employees who do not have a
manager.
9. Create a report to display the last name, salary, and commission of all employees who
earn commissions. Sort the data in descending order of salary and commissions. Use the
column’s numeric position in the ORDER BY clause.
10. Members of the HR department want to have more flexibility with the queries that you
are writing. They would like a report that displays the last name and salary of employees
who earn more than an amount that the user specifies after a prompt. Save this query to a
file named lab_01_10.sql.
11. The HR department wants to run reports based on a manager. Create a query that
prompts the user for a manager ID, and generates the employee ID, last name, salary, and
department for that manager’s employees. The HR department wants the ability to sort the
report on a selected column.
12. Display the last names of all employees where the third letter of the name is “a”.
13. Display the last names of all employees who have both an “a” and “e” in their last name.
14. Display the last name, job, and salary for all employees whose jobs are either that of a
sales representative or a stock clerk, and whose salaries are not equal to $2,500, $3,500, or
$7,000.
15. Modify lab_01_06.sql to display the last name, salary, and commission for all employees
whose commission is 20%. Save lab_01_06.sql as lab_01_15.sql again.
Solutions
Single‐Row Functions
1. Write a query to display the system date. Label the column Date.
2. The HR department needs a report to display the employee number, last name, salary,
and salary increased by 15.5% (expressed as a whole number) for each employee. Label
the column New Salary. Save your SQL statement in a file named lab_02_02.sql.
4. Modify your query in the lab_02_02.sql to add a column that subtracts the old salary from
the new salary. Label the column Increase. Save the contents of the file as lab_02_04.sql.
6. The HR department wants to find the duration of employment for each employee. For
each employee, display the last name and calculate the number of months between today
and the date on which the employee was hired. Label the column as MONTHS_WORKED.
Order your results by the number of months employed. The number of months must be
rounded to the closest whole number.
7. Create a query to display the last name and salary for all employees. Format the salary
to be 15 characters long, left-padded with the $ symbol. Label the column SALARY.
8. Create a query that displays the first eight characters of the employees’ last names, and
indicates the amounts of their salaries with asterisks. Each asterisk signifies a thousand
dollars. Sort the data in descending order of salary. Label the column
EMPLOYEES_AND_THEIR_SALARIES.
9. Create a query to display the last name and the number of weeks employed for all
employees in department 90. Label the number of weeks column as TENURE. Truncate the
number of weeks value to 0 decimal places. Show the records in descending order of the
employee’s tenure.
Solutions
Conditional Expressions
1. Create a report that produces the following for each employee: <employee last name>
earns <salary> monthly but wants <3 times salary.>. Label the column Dream Salaries.
2. Display each employee’s last name, hire date, and salary review date, which is the first
Monday after six months of service. Label the column REVIEW. Format the dates to
appear in a format that is similar to “Monday, the Thirty-First of July, 2000.”
3. Create a query that displays employees’ last names and commission amounts. If an
employee does not earn commission, show “No Commission.” Label the column COMM.
4.Using the CASE function, write a query that displays the grade of all employees based on
the value of the JOB_ID column, using the following codification: AD_PRES -> A,
ST_MAN -> B, IT_PROG -> C, SA_REP -> D, ST_CLERK -> E, None of the
above -> 0.
Solutions
Group Functions
1. Group functions work across many rows to produce one result per group.
PART II:
4. Find the highest, lowest, sum, and average salary of all employees. Label the columns
Maximum, Minimum, Sum, and Average, respectively. Round your results to the nearest
whole number. Save your SQL statement as lab_04_04.sql. Run the query.
5. Modify the query in lab_04_04.sql to display the minimum, maximum, sum, and
average salary for each job type. Save lab_04_04.sql as lab_04_05.sql again.
Run the statement in lab_04_05.sql.
6.Write a query to display the number of people with the same job. Generalize the query so
that the user in the HR department is prompted for a job title. Save the script to a file
named lab_04_06.sql.
7. Determine the number of managers without listing them. Label the column Number of
Managers.
8. Find the difference between the highest and lowest salaries. Label the column
DIFFERENCE.
9. Create a report to display the manager number and the salary of the lowest-paid
employee for that manager. Exclude anyone whose manager is not known. Exclude any
groups where the minimum salary is $6,000 or less. Sort the output in descending order
of salary.
10. Create a query to display the total number of employees and, of that total, the number of
employees hired in 2005, 2006, 2007, and 2008. Create appropriate column headings.
11. Create a matrix query to display the job, the salary for that job based on the department
number, and the total salary for that job, for departments 20, 50, 80, and 90, giving each
column an appropriate heading.
Solutions
Join Operations
1. Create a report for the HR department that displays employee last names, department
numbers, and all the employees who work in the same department as a given employee.
Give each column an appropriate label. Save the script to a file named lab_05_01.sql.
2. The HR department needs a report on job grades and salaries. To familiarize yourself
with the JOB_GRADES table, first show the structure of the JOB_GRADES table. Then
create a query that displays the name, job, department name, salary, and grade for all
employees.
3. The HR department wants to determine the names of all employees who were hired after
Davies. Create a query to display the name and hire date of any employee hired after
employee Davies.
4. The HR department needs to find the names and hire dates of all employees who were
hired before their managers, along with their managers’ names and hire dates. Save the
script to a file named lab_05_04.sql.
Solutions
Subqueries
1. The HR department needs a query that prompts the user for an employee’s last name.
The query then displays the last name and hire date of any employee in the same
department as the employee whose name the user supplies (excluding that employee).
2. Create a report that displays the employee number, last name, and salary of all
employees who earn more than the average salary. Sort the results in ascending order by
salary.
3. Write a query that displays the employee number and last name of all employees who
work in a department with any employee whose last name contains the letter “u.” Save
your SQL statement as lab_06_03.sql. Run your query.
4. The HR department needs a report that displays the last name, department number, and
job ID of all employees whose department location ID is 1700. Modify the query so that
the user is prompted for a location ID. Save this to a file named lab_06_04.sql.
5. Create a report for HR that displays the last name and salary of every employee who
reports to King.
6. Create a report for HR that displays the department number, last name, and job ID for
every employee in the Executive department.
7. Create a report that displays a list of all employees whose salary is more than the salary
of any employee from department 60.
8. Modify the query in lab_06_03.sql to display the employee number, last name, and
salary of all employees who earn more than the average salary, and who work in a
department with any employee whose last name contains the letter “u.” Save
lab_06_03.sql as lab_06_08.sql again. Run the statement in lab_06_08.sql.
Solutions
Subqueries
1. Write a query to display the last name, department number, and salary of any employee
whose department number and salary both match the department number and salary of
any employee who earns a commission.
2. Display the last name, department name, and salary of any employee whose salary and
job_ID match the salary and job_ID of any employee located in location ID 1700.
3. Create a query to display the last name, hire date, and salary for all employees who have
the same salary and manager_ID as Kochhar. Do not display Kochhar in the result set.
4. Create a query to display the employees who earn a salary that is higher than the salary
of all the sales managers (JOB_ID = 'SA_MAN'). Sort the results from the highest to the
lowest.
5. Display details such as the employee ID, last name, and department ID of those
employees who live in cities the names of which begin with T.
6. Write a query to find all employees who earn more than the average salary in their
departments. Display last name, salary, department ID, and the average salary for the
department. Sort by average salary and round to two decimals. Use aliases for the
columns retrieved by the query as shown in the sample output.
8. Write a query to display the last names of the employees who earn less than the average
salary in their departments.
9. Write a query to display the last names of the employees who have one or more
coworkers in their departments with later hire dates but higher salaries.
10. Write a query to display the employee ID, last names, and department names of all the
employees.
11. Write a query to display the department names of those departments whose total salary
cost is above one-eighth (1/8) of the total salary cost of the whole company. Use the
WITH clause to write this query. Name the query SUMMARY.
Solutions
Set Operators
• UNION operator
• INTERSECT operator
• MINUS operator
1. The HR department needs a list of department IDs for departments that do not contain the
job ID ST_CLERK. Use the set operators to create this report.
2. The HR department needs a list of countries that have no departments located in them.
Display the country IDs and the names of the countries. Use the set operators to create
this report.
3. Produce a list of jobs for departments 10, 50, and 20, in that order. Display the job ID and
department ID by using the set operators.
4. Create a report that lists the employee IDs and job IDs of those employees who currently
have a job title that is the same as their previous one (that is, they changed jobs but have
now gone back to doing the same job they did previously).