Database
Database
Introduction to SQL
ORDER BY Clause:
● 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:
● 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:
● 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.
● 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 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.
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:
● 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.
Given the structure and contents of the ConstructCo database shown in Figure 1, use
4. Write a query to count the number of Employees with the Assign charge more than
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.
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
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.
Figure 3: