You are on page 1of 7

Week-7 Tutorial

Introduction to SQL

1. Explain the difference between an ORDER BY clause and a GROUP BY clause.

ORDER BY Clause:

● The ORDER BY clause is used to sort the result-set in ascending or descending


order.

● It sorts the records based on one or more columns.

● By default, ORDER BY sorts the data in ascending order. If you want to sort
the data in descending order, you can use the DESC keyword.

● Example:
SELECT * FROM Employees ORDER BY Salary DESC;

GROUP BY Clause:

● The GROUP BY statement is often used with aggregate functions (COUNT,


MAX, MIN, SUM, AVG) to group the result-set by one or more columns.

● It arranges identical data into groups.

● Example:
SELECT COUNT(Employee_ID), Country FROM Employees GROUP BY
Country;

2. What is the difference between the COUNT aggregate function and the SUM
aggregate function?

COUNT Function:

● The COUNT function returns the number of rows that matches a specified
criterion.

● It is used when you need to know the total number of records that exist in a
table or that match certain criteria.

● Example:
SELECT COUNT(Employee_ID) FROM Employees;

SUM Function:

● The SUM function returns the total sum of a numeric column


.
● It is used when you need to aggregate data from a particular column for all
the rows or for the groups of rows.

● Example:
SELECT SUM(Salary) FROM Employees;

3. In a SELECT query, what is the difference between a WHERE clause and a HAVING
clause?

WHERE Clause:

● The WHERE clause is used to filter records before any groupings are made.

● It works on row’s data, not on aggregated data.

● It is used with SELECT, UPDATE, DELETE, etc. statements.

● Example:
SELECT * FROM Employees WHERE Salary > 50000; This SQL
statement selects all data from the “Employees” table where “Salary”
is greater than 50000.

HAVING Clause:

● The HAVING clause is used to filter records after the GROUP BY clause has
been applied.

● It works on aggregated data.

● It is used only with the SELECT statement and must follow the GROUP BY
clause if used.

● Example:
SELECT Department, COUNT(Employee_ID) FROM Employees GROUP
BY Department HAVING COUNT(Employee_ID) > 5; This SQL
statement lists the departments that have more than 5 employees.

4. What is a subquery, and what are its basic characteristics?

A subquery is a SQL query that is embedded within the WHERE or HAVING clause of
another SQL query. It is also referred to as an inner query, nested query, or both. As
a need to further limit the data that can be retrieved, it is used to return information
that will be used in the primary query.
The following are some basic characteristics of a subquery:

● Nestled inside another query: An SQL statement is invariably the container


for a subquery. It can be nested inside another subquery or inside a SELECT,
INSERT, UPDATE, or DELETE statement.

● First executed: The subquery runs before the primary query. The main query
makes use of the subquery's result.
● Return a single value: Depending on how it is used, a subquery must return
either a single value or a list of values. It must be used with operators that
can handle multiple values if it returns more than one value.

● Use of comparison operators: Subqueries can be used with IN, NOT IN,
EXISTS, and NOT EXISTS in addition to comparison operators like >, <, =, and
so forth.

Example:
SELECT Employee_Name
FROM Employees
WHERE Age> (SELECT AVG(Age) FROM Employees);

5. The ConstructCo database stores data for a consulting company that tracks all charges
to projects. The charges are based on the hours each employee works on each project. The
structure and contents of the ConstructCo database are shown in Figure 1.

Figure 1: ConstructCo Database


Note that the ASSIGNMENT table in Figure 1 stores the JOB_CHG_HOUR values as an
attribute (ASSIGN_CHG_HR) to maintain historical accuracy of the data. The
JOB_CHG_HOUR values are likely to change over time. In fact, a JOB_CHG_HOUR
change will be reflected in the ASSIGNMENT table. Naturally, the employee primary
job assignment might also change, so the ASSIGN_JOB is
also stored. Because those attributes are required to maintain the historical accuracy
of the data, they are not redundant.

Given the structure and contents of the ConstructCo database shown in Figure 1, use

SQL commands to answer the questions below:

1. Download the following file from the Moodle: 07_ConstructCo_mysql_txt.

2. Import file into XAMPP.

3. Write a query to count the number of projects.

SELECT COUNT(*) FROM PROJECT;

4. Write a query to count the number of Employees with the Assign charge more than
150.

SELECT COUNT(DISTINCT EMP_NUM) FROM ASSIGNMENT WHERE ASSIGN_CHG_HR


> 150;

5. Write the SQL code required to list the employee number, last name, first name,
and middle initial of all employees whose last names start with Smith. In other
words, the rows for both Smith and Smithfield should be included in the listing.
Sort the results by employee number.

SELECT EMP_NUM, EMP_LNAME, EMP_FNAME, EMP_INITIAL


FROM EMPLOYEE
WHERE EMP_LNAME LIKE 'Smith%'
ORDER BY EMP_NUM;

6. Using the EMPLOYEE, JOB, and PROJECT tables in the ConstructCo database, write
the SQL code that will join the EMPLOYEE and PROJECT tables using EMP_NUM as
the common attribute. Display the attributes shown in the results presented in
Figure 2, sorted by project value.

Figure 2:

SELECT *
FROM EMPLOYEE
JOIN PROJECT ON EMPLOYEE.EMP_NUM = PROJECT.EMP_NUM
ORDER BY PROJECT.PROJ_VALUE;

7. Write the SQL code that will produce the same information that was shown in
Figure 2, but sorted by the employee’s last name.

SELECT *
FROM EMPLOYEE
JOIN PROJECT ON EMPLOYEE.EMP_NUM=PROJECT.EMP_NUM
ORDER BY EMPLOYEE.EMP_LNAME;

8. Write the SQL code that will list only the distinct project numbers in the
ASSIGNMENT table, sorted by project number

SELECT DISTINCT PROJ_NUM


FROM ASSIGNMENT
ORDER BY PROJ_NUM;

9. Write the SQL code to validate the ASSIGN_CHARGE values in the ASSIGNMENT
table. Your query should retrieve the assignment number, employee number,
project number, the stored assignment charge (ASSIGN_CHARGE), and the
calculated assignment charge (calculated by multiplying ASSIGN_CHG_HR by
ASSIGN_HOURS). Sort the results by the assignment number.
SELECT ASSIGN_NUM, EMP_NUM, PROJ_NUM, ASSIGN_CHARGE, (ASSIGN_CHG_HR
* ASSIGN_HOURS) AS CALCULATED_CHARGE
FROM ASSIGNMENT
ORDER BY ASSIGN_NUM;

10. Using the data in the ASSIGNMENT table, write the SQL code that will yield the
total number of hours worked for each employee and the total charges stemming
from those hours worked, sorted by employee number. The results of running that
query are shown in Figure3.

SELECT EMP_NUM, SUM(ASSIGN_HOURS) AS TOTAL_HOURS, SUM(ASSIGN_CHARGE)


AS
TOTAL_CHARGE
FROM ASSIGNMENT
GROUP BY EMP_NUM
ORDER BY EMP_NUM;

Figure 3:

You might also like