Simple Aggregate Queries
1) SELECT * FROM Course → Selects all columns from the Course table.
WHERE CrsDesc LIKE '%Business%' - Filters the courses where CrsDesc (course
description) contains "Business" anywhere.
The % is a wildcard that represents any number of characters (including none).
%Business% means "Business" can be at the start, middle, or end of the
description.
The LIKE operator is used for pattern matching (not = because = looks for an exact
match).
The search is not case-sensitive (it will match "business", "BUSINESS", "Business",
etc.).
To find courses that do not contain "Business", use NOT LIKE '%Business%'.
If you don’t use a wildcard (%) at the beginning of the string in a LIKE query, the
search will only find course descriptions that start with "Business."
For example, if you write:
WHERE CrsDesc LIKE 'Business%'
This means:
The course description must start with "Business". Any characters can follow
"Business" (because of % at the end). It won’t match descriptions where
"Business" appears in the middle or at the end.
In contrast, if you use %Business%, the search will find "Business" anywhere in the
text.
2) SELECT * FROM Course WHERE CourseNo LIKE '___3%'
This uses the underscore (_) wildcard as a placeholder for a single character
in a specific position. The three underscores represent any three characters
in the first three positions of the course number, and the "3" specifies that
the fourth character must be "3". The % wildcard allows any characters to
follow after the "3". This approach targets course numbers where the
fourth character is "3", regardless of what follows. The query demonstrates
how using wildcards can precisely locate patterns in a string based on
specific character positions, although it also highlights a potential issue in
database design—relying on course numbers to encode information like
semesters may not be as flexible as storing such details in separate fields.
3) Date values can be expressed in many different formats some of which are
specific to the particular DBMS being used. We will therefore only focus on
a single universally accepted format of ‘YYYY/MM/DD’. If you find other
formats that work for you that is perfectly acceptable.
4) The COUNT(*) function is used to count the total number of rows in a table,
without focusing on specific column values. In the example, SELECT
COUNT(*) AS "Number of Students" FROM Student, the function counts all
the rows in the Student table, returning the total number of students. The
wildcard * is used because we’re counting rows, not values in a specific
column. The result is then given an alias, "Number of Students", for
readability.
5) SUM(): Adds up the values in a numeric column. Example: SELECT
SUM(FacSalary) AS "Total Value of Salaries" FROM Faculty; This returns the
total sum of all faculty salaries in the Faculty table.
6) AVG(): Calculates the average (mean) of a numeric column. Example:
SELECT AVG(StdGPA) AS "Average GPA" FROM Student WHERE StdMajor =
'ITM'; This calculates the average GPA of students who are in the ITM
major.
7) MAX(): Finds the highest value in a column. Example: SELECT MAX(StdGPA)
AS "Highest GPA" FROM Student WHERE StdMajor = 'ITM'; This finds the
highest GPA among students in the ITM major.
8) MIN(): Finds the lowest value in a column. Example: SELECT MIN(StdGPA)
AS "Lowest GPA" FROM Student WHERE StdMajor = 'ITM'; This finds the
lowest GPA among students in the ITM major.
For each of these functions, an alias is used (like "Total Value of Salaries",
"Average GPA", etc.) to make the result more readable. The key point is that
aggregate functions like SUM(), AVG(), MAX(), and MIN() operate on numeric
columns, but can also be applied to dates and strings (with different
interpretations, like the most recent date or alphabetically last string).
9) COUNT(DISTINCT column): Counts only the unique values in a specific
column. For example, SELECT COUNT(DISTINCT StdSSN) FROM Enrollment
counts how many different students are enrolled, even if a student is
enrolled in multiple courses. The DISTINCT keyword ensures that each
student is counted once, no matter how many enrollments they have.
10) COUNT(column): If you count a specific column, SQL will ignore rows with
NULL values in that column. For example, SELECT COUNT(FacSSN) FROM
Offering counts only the rows where a faculty member is assigned (i.e.,
where FacSSN is not NULL). This is an alternative way to count only rows
where a specific condition is met, without needing a separate WHERE
clause. You could also explicitly test for NULL using WHERE FacSSN IS NOT
NULL.
11) NULL handling: When counting rows with a specific condition, NULL
values must be handled carefully. For example, if you want to count how
many offerings don't have a faculty member assigned, you must check for
NULL using WHERE FacSSN IS NULL.
These examples show how to use COUNT() to count rows or unique values,
with the ability to filter for specific conditions, like non-NULL values or
unique occurrences.