Joining Queries and Functions
Joining Queries and Functions
UNION
The UNION operator is a powerful tool in SQL that enables you to merge the results of multiple
queries into a unified list. When employing UNION, it's essential to follow certain principles to
ensure smooth and effective data merging.
Firstly, all queries being combined must possess the same number of columns in the same
sequential order. This consistency ensures coherence in the resulting dataset.
Moreover, it's crucial to maintain uniformity in data types across corresponding columns in the
queries being merged. This means that the data types should either match exactly or be
compatible with one another to prevent data type conflicts during the merging process.
One key feature of UNION is its automatic removal of duplicate rows from the final result set. This
eliminates the need for explicitly using the DISTINCT command to filter out duplicates.
However, if retaining duplicate rows is desired, the UNION ALL operator can be explicitly used.
Unlike UNION, UNION ALL preserves duplicate rows, if any, in the final result set. Additionally,
since UNION ALL doesn't need to handle duplicate removal, it generally performs faster than its
DISTINCT counterpart.
To illustrate the concept visually, one can envision a Venn diagram that depicts how the UNION
operator combines the results of two distinct queries, showcasing the overlap and unique
elements from each dataset.
IT 101 - Database Design and Implementation
10. Joining Queries and Functions
Let's delve into an example scenario to elucidate the usage of the UNION operator further.
Suppose we aim to retrieve the names and surnames of both employees and customers within a
single query. We can accomplish this by employing the UNION ALL operator as follows:
FROM employees e
In this query, we're selecting the first name and last name columns from the customers table
(aliased as 'c') and employees table (aliased as 'e'), respectively, and combining them using UNION
ALL. This approach ensures that all employee and customer names are included in the final result
set, irrespective of duplicates.
To reiterate, while UNION ALL returns all entities from the combined queries, including
duplicates, the default behavior of UNION DISTINCT ensures that only unique values for the
specified columns are returned. This distinction is crucial in determining which operator to utilize
based on the specific requirements of your data retrieval needs.
In MySQL, although the FULL OUTER JOIN isn't directly supported as in some other SQL
implementations, you can achieve similar functionality by leveraging the UNION operator along
with a combination of JOIN operations. Let's delve into an example scenario involving three tables:
students, courses, and student_courses, where the student_courses table establishes a
many-to-many relationship between students and courses.
Firstly, let's explore two distinct scenarios. In the first scenario, our aim is to retrieve all records
from both the students and courses tables. In the second scenario, we're interested in identifying
students who are not enrolled in any courses and courses that have no students enrolled in them.
For the first scenario, where we want all records from both tables, we can utilize a combination of
LEFT JOIN and RIGHT JOIN operations, followed by a UNION:
In this query, we're using LEFT JOIN to retrieve all students along with their enrolled courses and
RIGHT JOIN to capture all courses along with the students enrolled in them. The UNION operator
combines the results of these two sets to produce a unified list of student-course combinations.
IT 101 - Database Design and Implementation
10. Joining Queries and Functions
In this modified query, we're selecting students with no enrolled courses by checking for NULL
values in the course_id column after the LEFT JOIN operation. Similarly, we're identifying courses
with no enrolled students by examining NULL values in the student_id column following the
RIGHT JOIN operation. The UNION operator then combines the results of these two sets to
provide a comprehensive view of students without courses and courses without students.
When you use a JOIN operation in SQL, it's akin to merging two tables side-by-side, effectively
bringing together related data points from each table into new columns. This process expands the
width of the resulting table, providing a comprehensive view of interconnected information.
Think of it as if you're piecing together two puzzle pieces that fit snugly side-by-side, revealing a
broader picture of your data. Each row in the resulting table represents a combination of
corresponding rows from the original tables, with related data elements aligned in adjacent
columns.
Conversely, when you employ the UNION operator, it operates more like stacking the tables on
top of each other. Instead of widening the table as in a JOIN, UNION combines the results row by
row, effectively appending rows from one table to the end of another.
Picture it as if you're compiling two stacks of papers, one on top of the other, to create a taller
stack. Each row in the final result represents a distinct entry from either table, with the UNION
operation seamlessly amalgamating the rows from both tables to form a comprehensive list.
In essence, while JOIN expands the width of your data by aligning related columns, UNION
extends the length of your data by appending rows, ultimately providing different perspectives on
your dataset.
IT 101 - Database Design and Implementation
10. Joining Queries and Functions
INTERSECT
The INTERSECT operator in SQL is a powerful tool for comparing the result sets of two queries
and returning only the common rows shared between them. When using INTERSECT, there are
several important considerations to keep in mind to ensure smooth and accurate operation:
● Consistent Columns: All the queries being combined must feature the same number of
columns in the same sequential order. This ensures that the comparison between the
result sets is meaningful and accurate.
● Compatible Data Types: The data types in corresponding columns across queries should be
either identical or compatible with each other. This prevents any potential conflicts or
errors during the comparison process.
● Automatic Duplicate Removal: INTERSECT automatically eliminates duplicate rows from
the final result set. Therefore, there's no need to explicitly use the DISTINCT command for
duplicate removal.
● INTERSECT ALL vs. INTERSECT DISTINCT: By default, INTERSECT performs distinct
comparisons, removing duplicate rows. However, if explicit retention of duplicate rows is
desired, the INTERSECT ALL operator can be utilized. INTERSECT ALL performs faster
than INTERSECT DISTINCT since it doesn't need to handle duplicate removal.
SELECT a.first_name
FROM actor a
INTERSECT
SELECT c.first_name
FROM customer c;
In this query, we're selecting the first names of actors and customers and using the INTERSECT
operator to identify and return only the common first names between the two tables.
IT 101 - Database Design and Implementation
10. Joining Queries and Functions
By understanding these nuances and applying them effectively, you can leverage the INTERSECT
operator to efficiently identify and extract common elements from your datasets in SQL.
EXCEPT
The EXCEPT operator in SQL is a valuable tool for comparing the result sets of two queries and
retrieving rows that are present in the first query but absent in the second. When utilizing
EXCEPT, it's essential to adhere to several key considerations to ensure accurate and effective
operation:
● Consistent Columns: All the queries being combined must feature the same number of
columns in the same sequential order. This ensures that the comparison between the
result sets is meaningful and aligned.
● Compatible Data Types: The data types in corresponding columns across queries should
be either identical or compatible with each other. This prevents any potential conflicts or
errors during the comparison process.
● Automatic Duplicate Removal: EXCEPT automatically eliminates duplicate rows from the
final result set. Therefore, there's no need to explicitly use the DISTINCT command for
duplicate removal.
● EXCEPT ALL vs. EXCEPT DISTINCT: By default, EXCEPT performs distinct
comparisons, removing duplicate rows. However, if explicit retention of duplicate rows is
desired, the EXCEPT ALL operator can be utilized. EXCEPT ALL performs faster than
EXCEPT DISTINCT since it doesn't need to handle duplicate removal.
Now, let's explore an example to demonstrate the usage of EXCEPT. Suppose we aim to
retrieve all names from the actor table that are not present in the customers table in the sakila
database:
SELECT a.first_name
FROM actor a
EXCEPT
SELECT c.first_name
FROM customer c;
IT 101 - Database Design and Implementation
10. Joining Queries and Functions
In this query, we're selecting the first names from the actor table and using the EXCEPT
operator to exclude any first names that are also found in the customers table. As mentioned
earlier, the default behavior of EXCEPT is DISTINCT, meaning it automatically removes
duplicate entries from the result set. However, if retaining duplicates is desired, the ALL keyword
can be added after the EXCEPT operator to specify EXCEPT ALL.
FUNCTIONS
In MySQL, functions serve as a powerful set of built-in tools designed to execute specific tasks
efficiently. These functions play a crucial role in manipulating data, performing calculations,
formatting output, and more. MySQL offers a diverse array of functions across various
categories to address a wide range of requirements. Here are some common categories of
MySQL functions:
● String Functions: These functions are tailored to manipulate string values, allowing users
to perform tasks such as concatenation, substring extraction, character manipulation,
and pattern matching. Examples include CONCAT(), SUBSTRING(), UPPER(),
LOWER(), and REPLACE().
● Date and Time Functions: MySQL provides a comprehensive suite of functions to handle
date and time values effectively. These functions enable users to perform operations
such as date arithmetic, date formatting, extraction of components (e.g., year, month,
day), and timezone conversions. Popular examples include DATE_FORMAT(), NOW(),
DATE_ADD(), and TIMESTAMPDIFF().
● Mathematical Functions: MySQL offers a rich collection of mathematical functions to
facilitate numeric calculations. These functions encompass basic arithmetic operations
(e.g., addition, subtraction, multiplication, division), rounding, absolute value calculation,
exponentiation, and trigonometric functions. Prominent examples include ROUND(),
ABS(), POW(), and SIN().
● Control Flow Functions: Control flow functions empower users to manage the flow of
execution within SQL queries based on specified conditions. These functions include
conditional statements (e.g., IF(), CASE), branching constructs (e.g., IFNULL(),
NULLIF()), and loop control functions (e.g., WHILE()). They allow for conditional logic
and branching within SQL statements to handle varying scenarios effectively.
● Comparison Functions: Comparison functions play a pivotal role in evaluating and
comparing values within SQL queries. They enable users to perform equality checks,
inequality checks, and sorting operations. Common examples include IF(), CASE, IS
NULL, IS NOT NULL, and COALESCE().
● By leveraging these categories of functions, MySQL users can enhance the functionality
and flexibility of their queries, thereby achieving more robust data manipulation and
analysis capabilities. Whether it's manipulating strings, handling date and time values,
performing mathematical calculations, controlling flow based on conditions, or comparing
data, MySQL functions offer a versatile toolkit for diverse data processing needs.
IT 101 - Database Design and Implementation
10. Joining Queries and Functions
STRING FUNCTIONS
In MySQL, string manipulation functions are invaluable tools for extracting specific portions of
text from strings, transforming case, replacing substrings, trimming whitespace, and more. Let's
explore some commonly used string manipulation functions in MySQL:
CASE: The CASE function allows you to evaluate conditions and return a result based on those
conditions. It operates similar to a switch statement in other programming languages. The
syntax is as follows:
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE default_result
END
This function returns result1 if condition1 is satisfied, result2 if condition2 is satisfied, and so on.
If none of the conditions are met, it returns the default_result.
IF: The IF function evaluates a condition and returns one value if the condition is true and
another value if the condition is false. The syntax is:
IFNULL: The IFNULL function checks if an expression is NULL. If the expression is not NULL, it
returns the expression; otherwise, it returns an alternate value. The syntax is:
IFNULL(expression, alt_value)
NULLIF: The NULLIF function compares two expressions. If the expressions are equal, it
returns NULL; otherwise, it returns the first expression. This function is useful for handling cases
where you want to return NULL instead of a specific value under certain conditions. The syntax
is:
NULLIF(expr1, expr2)
IT 101 - Database Design and Implementation
10. Joining Queries and Functions
DATE FUNCTIONS
Date functions in MySQL are essential tools for manipulating dates and timestamps, performing
calculations, and retrieving current date and time information. Let's explore some commonly
used date functions:
● CURDATE() / CURRENT_DATE() / CURRENT_DATE: Return the current date.
● CURRENT_TIME() / CURTIME(): Return the current time.
● NOW() / CURRENT_TIMESTAMP() / CURRENT_TIMESTAMP / LOCALTIME() /
LOCALTIMESTAMP(): Return the current date and time.
● SYSDATE(): Return the time at which it executes.
● UTC_TIMESTAMP(): Return the current UTC date and time.
● UTC_DATE(): Return the current UTC date.
● UTC_TIME(): Return the current UTC time.
● ADDTIME(datetime, addtime): Add a time interval to a time value or datetime value.
● DATE_ADD(date, INTERVAL value addunit) / ADDDATE(): Add a time value to a date.
● DATE_SUB(date, INTERVAL value addunit): Subtract a time value (interval) from a date.
● DATEDIFF(date1, date2): Return the difference in days between two date values.
● TIMEDIFF(time1, time2): Return the difference of two time values.
● TIMESTAMPADD(unit,interval,datetime_expr): Add or subtract an interval from a
timestamp or date.
● TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2): Return the difference between
two timestamp values.
● TIME_TO_SEC(time): Return the number of seconds from a time argument.
● TO_DAYS(date): Return a day number (the number of days since year 0) from a given
date.
● CONVERT_TZ(date, from_tz,to_tz): Convert a datetime value from one time zone to
another.
● FROM_DAYS(N): Convert a numeric day count into a date.
● STR_TO_DATE(str,format): Convert a string to date.
● FROM_UNIXTIME(unix_timestamp, [format]): Convert UNIX timestamps into a readable
date and time format.
● UNIX_TIMESTAMP(date): Convert a datetime to a UNIX timestamp.
These functions provide MySQL users with versatile tools for handling date and time
information, performing calculations, and converting between different date and time formats,
thereby facilitating effective date-related operations within SQL queries.
MATH FUNCTIONS
In MySQL, mathematical functions are essential for performing various calculations and
transformations on numeric values. Here are some commonly used math functions: