0% found this document useful (0 votes)
24 views11 pages

Joining Queries and Functions

Uploaded by

arnela.sokolic1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views11 pages

Joining Queries and Functions

Uploaded by

arnela.sokolic1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

IT 101 - Database Design and Implementation

10. Joining Queries and Functions

10. 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:

SELECT c.contactFirstName, c.contactLastName


FROM customers c
UNION ALL
SELECT e.firstName, e.lastName
IT 101 - Database Design and Implementation
10. Joining Queries and Functions

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.

FULL OUTER JOIN

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:

SELECT s.student_id, s.student_name, c.course_id, c.course_name


FROM students s
LEFT JOIN enrollments e ON s.student_id = e.student_id
LEFT JOIN courses c ON e.course_id = c.course_id
UNION
SELECT s.student_id, s.student_name, c.course_id, c.course_name
FROM students s
RIGHT JOIN enrollments e ON s.student_id = e.student_id
RIGHT JOIN courses c ON e.course_id = c.course_id;

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

Now, moving on to the second scenario, where we want to


identify students not taking any courses and courses without any students enrolled, we can modify
our query to include a WHERE clause to filter out the NULL values:

SELECT s.student_id, s.student_name, c.course_id, c.course_name


FROM students s
LEFT JOIN enrollments e ON s.student_id = e.student_id
LEFT JOIN courses c ON e.course_id = c.course_id
WHERE c.course_id IS NULL
UNION
SELECT s.student_id, s.student_name, c.course_id, c.course_name
FROM students s
RIGHT JOIN enrollments e ON s.student_id = e.student_id
RIGHT JOIN courses c ON e.course_id = c.course_id
WHERE s.student_id IS NULL;

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

As mentioned earlier, the default behavior of INTERSECT 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 INTERSECT operator to
specify INTERSECT ALL.

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:

● SUBSTRING(string, start, length) / SUBSTRING(string FROM start FOR length):


Extracts a substring from a given string starting at a specified position and of a specified
length.
● SUBSTRING_INDEX(string, delimiter, number): Extracts a substring from a string based
on a delimiter and occurrence number.
● LEFT(string, number_of_chars): Returns a specified number of characters from the
beginning of a string.
● RIGHT(string, number_of_chars): Returns a specified number of characters from the end
of a string.
● MID(string, start, length): Extracts a substring from the middle of a string. (MID() is a
synonym for SUBSTRING())
● LOCATE(substring, string, start) / POSITION(substring IN string) / INSTR(string1,
string2): Find the position of a substring within a string.
● UPPER(string) / LOWER(string): Convert a string to uppercase or lowercase,
respectively.
● REPLACE(string, substring, new_string): Replaces all occurrences of a substring in a
string with a new substring.
● TRIM(string) / LTRIM(string) / RTRIM(string): Remove leading, trailing, or both leading
and trailing spaces from a string, respectively.
● REPEAT(string, number): Repeat a string a specified number of times.
● REVERSE(string): Reverse the characters in a string.
● INSERT(string, position, number, string2): Replace a substring within a string with a new
substring.
● SPACE(number): Return a string consisting of spaces.
● ASCII(string): Return the ASCII value of the leftmost character of a string.
● CHAR(): Convert an ASCII value to a character.
● LENGTH() / CHAR_LENGTH() / OCTET_LENGTH() / BIT_LENGTH() /
CHARACTER_LENGTH(): Return the length of a string in bytes, characters, octets, bits,
or characters, respectively.
● BIT_COUNT(): Count the number of bits in a binary string.
● STRCMP(): Compare two strings and return their relative order.
● LPAD(string, length, lpad_string) / RPAD(string, length, lpad_string): Left-pad or
right-pad a string with a set of characters to a specified length.
● COALESCE(): Return the first non-NULL arguments, useful for substitution of NULL
values.
IT 101 - Database Design and Implementation
10. Joining Queries and Functions

● GREATEST() / LEAST(): Take multiple arguments


and return the greatest and least values, respectively.
● ISNULL(): Return 1 if the argument is NULL, otherwise, return zero.
● These functions provide MySQL users with a robust toolkit for manipulating strings,
performing comparisons, and handling NULL values efficiently, thereby enhancing data
processing capabilities within SQL queries.

CONTROL FLOW FUNCTIONS


In MySQL, conditional functions play a crucial role in controlling the flow of execution and
determining values based on specified conditions. Here are some commonly used conditional
functions:

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:

IF(condition, value_if_true, value_if_false)


If the condition is true, it returns value_if_true; otherwise, it returns value_if_false.

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:

● ABS(): Returns the absolute value of a number.


● CEIL(): Returns the smallest integer value greater than or equal to the input number (n).
IT 101 - Database Design and Implementation
10. Joining Queries and Functions

● FLOOR(): Returns the largest integer value not


greater than the argument.
● MOD(): Returns the remainder of a number divided by another.
● ROUND(): Rounds a number to a specified number of decimal places.
● TRUNCATE(): Truncates a number to a specified number of decimal places.
● ACOS(n): Returns the arc cosine of n, or null if n is not in the range -1 to 1.
● ASIN(n): Returns the arcsine of n, which is the value whose sine is n. Returns null if n is
not in the range -1 to 1.
● ATAN(): Returns the arctangent of n.
● ATAN2(n,m) / ATAN(m,n): Returns the arctangent of the two variables n and m.
● CONV(n,from_base,to_base): Converts a number between different number bases.
● COS(n): Returns the cosine of n, where n is in radians.
● COT(n): Returns the cotangent of n.
● CRC32(): Computes a cyclic redundancy check value and returns a 32-bit unsigned
value.
● DEGREES(n): Converts radians to degrees of the argument n.
● EXP(n): Raises e to the power of n.
● LN(n): Returns the natural logarithm of n.
● LOG(n): Returns the natural logarithm of the first argument.
● LOG10() - Returns the base-10 logarithm of the argument
● LOG2() - Returns the base-2 logarithm of the argument
● PI() - Returns the value of PI
● POW() - Returns the argument raised to the specified power
● POWER() - Returns the argument raised to the specified power
● RADIANS() - Returns argument converted to radians
● RAND() - Returns a random floating-point value between 0 and 1.
● SIGN(n) - Returns the sign of n that can be -1, 0, or 1 depending on whether n is
negative, zero, or positive.
● SIN(n) - Returns the sine of n
● SQRT(n) - Returns the square root of n
● TAN(n) Returns the tangent of n

You might also like