Podcast
Questions and Answers
SQL is based on relational algebra.
SQL is based on relational algebra.
True (A)
Which SQL version introduced outer joins?
Which SQL version introduced outer joins?
- SQL99
- SQL89
- SQL2003
- SQL92 (correct)
What clause is required for outer joins introduced by SQL92?
What clause is required for outer joins introduced by SQL92?
ON
The words 'INNER' and '______' are optional in SQL join statements.
The words 'INNER' and '______' are optional in SQL join statements.
Match the SQL function with its description:
Match the SQL function with its description:
Which function returns the starting position of a substring within a string?
Which function returns the starting position of a substring within a string?
The function ________ takes a string, a starting position, and a length, and returns a substring.
The function ________ takes a string, a starting position, and a length, and returns a substring.
The concatenation operator ||
is supported in MySQL.
The concatenation operator ||
is supported in MySQL.
Which function truncates a number after a specified number of decimal places?
Which function truncates a number after a specified number of decimal places?
Which function returns the remainder of one number divided by another?
Which function returns the remainder of one number divided by another?
Multi-row functions are also known as aggregate functions.
Multi-row functions are also known as aggregate functions.
Which aggregate function calculates the average value in a column, ignoring NULL values?
Which aggregate function calculates the average value in a column, ignoring NULL values?
The ________ function returns the number of rows in a table.
The ________ function returns the number of rows in a table.
What do the MAX()
and MIN()
functions return?
What do the MAX()
and MIN()
functions return?
The SUM function can only be used on numeric columns.
The SUM function can only be used on numeric columns.
Which SQL clause is used to group rows that have the same values in specified columns into summary rows?
Which SQL clause is used to group rows that have the same values in specified columns into summary rows?
When using a GROUP BY
clause, should all attributes that are not part of the aggregate function be included?
When using a GROUP BY
clause, should all attributes that are not part of the aggregate function be included?
The ________ clause is used to filter results after grouping has been performed.
The ________ clause is used to filter results after grouping has been performed.
The WHERE
clause filters data after the GROUP BY
clause is applied.
The WHERE
clause filters data after the GROUP BY
clause is applied.
Which clause is used to sort the rows?
Which clause is used to sort the rows?
The SQL92 standard has exactly the same support for JOIN types as SQL89.
The SQL92 standard has exactly the same support for JOIN types as SQL89.
In SQL, what function is used to return a string with all lowercase letters?
In SQL, what function is used to return a string with all lowercase letters?
In SQL, which of these aggregate functions returns the total of all values in a given column?
In SQL, which of these aggregate functions returns the total of all values in a given column?
In SQL, the ________
keyword is used to eliminate duplicate values in an aggregate function.
In SQL, the ________
keyword is used to eliminate duplicate values in an aggregate function.
In SQL, other clauses can accomplish sorting, so the ORDER BY
is not that important.
In SQL, other clauses can accomplish sorting, so the ORDER BY
is not that important.
What is a potential benefit of using highly abstract tables?
What is a potential benefit of using highly abstract tables?
What symbol is used for the concatenation operator in SQL?
What symbol is used for the concatenation operator in SQL?
When joining tables and the attributes used are named the same, can use ____________. (SQL 92 only)
When joining tables and the attributes used are named the same, can use ____________. (SQL 92 only)
The UNIQUE
constraint enforces that all values in a column are different.
The UNIQUE
constraint enforces that all values in a column are different.
What is a downside of highly abstract tables?
What is a downside of highly abstract tables?
Match the following SQL commands with their function.
Match the following SQL commands with their function.
Given a database with tables artist
, song
, and cd_makeup
, which step is most important when performing multi-table joins to list the Artist name and the CD titles the artist is included in?
Given a database with tables artist
, song
, and cd_makeup
, which step is most important when performing multi-table joins to list the Artist name and the CD titles the artist is included in?
When doing multi-table joins in SQL, it is generally more efficient to join all tables simultaneously rather than joining two at a time.
When doing multi-table joins in SQL, it is generally more efficient to join all tables simultaneously rather than joining two at a time.
In SQL, what function is used to extract a substring from a string, given a starting position and the number of characters to extract?
In SQL, what function is used to extract a substring from a string, given a starting position and the number of characters to extract?
In SQL, the ______ function transforms a string to uppercase.
In SQL, the ______ function transforms a string to uppercase.
In SQL, SQL statements are case sensitive and must be typed in the correct case for it to be run.
In SQL, SQL statements are case sensitive and must be typed in the correct case for it to be run.
Which SQL clause is used to limit the records before a GROUP BY is performed?
Which SQL clause is used to limit the records before a GROUP BY is performed?
In SQL, what does the term INNER JOIN refer to?
In SQL, what does the term INNER JOIN refer to?
In SQL, the keyword ________ indicates that every value in a given column has its own value.
In SQL, the keyword ________ indicates that every value in a given column has its own value.
In multi-table joins, it is okay for multiple records to share a common value.
In multi-table joins, it is okay for multiple records to share a common value.
What is the correct order of operations to run the code properly?
What is the correct order of operations to run the code properly?
Flashcards
UNIQUE constraint
UNIQUE constraint
A constraint ensuring all values in a column are unique.
INNER and OUTER
INNER and OUTER
Words 'INNER' and 'OUTER' are optional in SQL joins.
Outer Joins
Outer Joins
Introduced with SQL92, requires ON
clause.
Inner Joins
Inner Joins
Signup and view all the flashcards
Multi-table Joins
Multi-table Joins
Signup and view all the flashcards
Building Multi-table Joins
Building Multi-table Joins
Signup and view all the flashcards
SQL92
SQL92
Signup and view all the flashcards
SQL89
SQL89
Signup and view all the flashcards
LOWER() function
LOWER() function
Signup and view all the flashcards
UPPER() function
UPPER() function
Signup and view all the flashcards
LENGTH() function
LENGTH() function
Signup and view all the flashcards
INSTR() function
INSTR() function
Signup and view all the flashcards
SUBSTR() function
SUBSTR() function
Signup and view all the flashcards
CONCAT() function
CONCAT() function
Signup and view all the flashcards
Concatenation Operator (||)
Concatenation Operator (||)
Signup and view all the flashcards
ROUND(n,m) function
ROUND(n,m) function
Signup and view all the flashcards
TRUNCATE(n,m) function
TRUNCATE(n,m) function
Signup and view all the flashcards
MOD(n,m) function
MOD(n,m) function
Signup and view all the flashcards
Multi-Row Functions
Multi-Row Functions
Signup and view all the flashcards
AVG() function
AVG() function
Signup and view all the flashcards
COUNT() function
COUNT() function
Signup and view all the flashcards
MAX()/MIN() functions
MAX()/MIN() functions
Signup and view all the flashcards
SUM() function
SUM() function
Signup and view all the flashcards
GROUP BY clause
GROUP BY clause
Signup and view all the flashcards
HAVING clause
HAVING clause
Signup and view all the flashcards
ORDER BY clause
ORDER BY clause
Signup and view all the flashcards
Highly Abstract Tables
Highly Abstract Tables
Signup and view all the flashcards
Study Notes
- Week 13 introduces database and data modeling concepts.
SQL Basics
- SQL (Structured Query Language) is rooted in relational algebra.
- Queries employ combinations of relational algebra operations.
E-R Diagram
- E-R Diagram example includes SONG, ARTIST, and CD entities
- SONG contains song_id, song_title, length, and artist_id
- ARTIST contains artist_id and artist
- CD contains cd_id, cd_title
- CD_MAKEUP contains cd_id and song_id
- A unique constraint is shown for artist->artist_id.
- Functional dependencies (FDs) are defined for each entity.
SQL JOIN Shortcuts
- "INNER" and "OUTER" are optional keywords in SQL JOIN syntax.
- Outer joins were introduced with SQL92 and necessitate the "ON" clause.
- Inner joins existed since SQL89 and don't need the "ON" clause.
SQL 89 vs SQL 92: INNER JOIN
- SQL 89/92 equivalent statements use INNER JOIN.
- SQL 92 example:
SELECT artist.artist, song.song_title FROM artist INNER JOIN song ON artist.artist_id = song.artist_id;
- SQL 89 equivalent:
SELECT artist.artist, song.song_title FROM artist, song WHERE artist.artist_id = song.artist_id;
SQL 92 OUTER JOIN
- The SQL 92 equivalent statement uses OUTER JOIN:
SELECT artist.artist, song.song_title FROM song RIGHT OUTER JOIN artist ON song.artist_id = artist.artist_id;
- No SQL 89 equivalent exists for OUTER JOINS.
SQL Shortcuts & Table Aliases
- Most DBMS support table aliases (SQL 89 or SQL 92),
- Example:
SELECT art.artist, s.song_title FROM song as s RIGHT JOIN artist art ON s.artist_id = art.artist_id;
USING()
can be employed when joining tables with identically named attributes (SQL 92 only).- Example:
SELECT art.artist, s.song_title FROM song as s RIGHT JOIN artist art USING(artist_id);
Multi-Table Joins
- SQL allows joining as many tables as necessary.
- The physical E-R diagram should be referenced while writing the query.
- Tables are joined two at a time, following the established path in the diagram.
- It is useful to consider how the structure of which table is added.
- Example: Listing the Artist name and CD Titles the artist is included in
Multi-Table Join Example (Artist Name and CD Titles)
- Determine attributes and "path" start:
SELECT artist.artist, cd.cd_title FROM artist
- Join the next table in the path:
SELECT artist.artist, cd.cd_title FROM artist INNER JOIN Songs ON Artists.ArtistID = Songs. ArtistID
- Join further tables in the path:
SELECT artist.artist, cd.cd_title FROM (artist INNER JOIN song ON artist.artist_id = song.artist_id) INNER JOIN cd_makeup ON song.song_id = cd_makeup.song_id
- Final join to complete the path:
SELECT artist.artist, cd.cd_title FROM ((artist INNER JOIN song ON artist.artist_id = song.artist_id) INNER JOIN cd_makeup ON song.song_id = cd_makeup.song_id) INNER JOIN cd ON cd_makeup.cd_id=cd.cd_id;
SQL 89 vs. SQL 92 - JOINs
- SQL 92 fully supports all
JOIN
types. - The SQL 89 standard only supports
INNER JOIN
Beyond JOINs
- It can show the artist and CD title for CDs that include the artist ' Smash Mouth', but require slightly different syntax for SQL92 and SQL89
SQL Functions
- SQL functions avoid the need to reinvent the wheel.
- Functions exist, are easy to use, and efficient.
- Functions are SQL features and operate on single rows or sets of data, enabling data modification and statistical analysis.
Single-Row Functions
- Character functions include:
LOWER()
,UPPER()
,LENGTH()
,INSTR()
,SUBSTR()
,CONCAT()
- Numeric functions:
ROUND()
,TRUNCATE()
,MOD()
LOWER/UPPER Functions
LOWER(s)
: Returns strings
in all lowercase.UPPER(s)
: Returns strings
in all uppercase.- Usefulness lies in case-insensitive string operations.
- MySQL, although not case sensitive, recommends using these functions when case should not impact results.
LENGTH Function
LENGTH(s)
: Returns the number of characters in strings
.- Very useful when doing string comparisons
INSTR Function
INSTR(s1,s2)
(orLOCATE(s2,s1)
in MySQL) checks if strings2
is in strings1
.- Returns the start position of the first match; returns 0 if
s2
is not ins1
.
SUBSTR Function
SUBSTR(s,n,m)
: Returns a substring ofs
starting at positionn
and of lengthm
.
CONCAT Function
CONCAT(s1,s2)
: Appends strings2
to strings1
.- MySQL allows CONCAT overloads
Concatenation Operator
||
is a SQL concatenation operator.- Not supported in MySQL
ROUND/TRUNCATE Functions
ROUND(n,m)
: Rounds numbern
tom
decimal places.TRUNCATE(n,m)
: Truncates numbern
afterm
decimal places.
MOD Function
MOD(n,m)
: Returns the remainder whenn
is divided bym
.
Multi-Row Functions
- Multi-row functions are also known as Aggregate Functions
- These functions work a column of data and return a single result
- Includes
AVG()
,COUNT()
,MAX()
,MIN()
, andSUM()
AVG Function
AVG(column)
: Returns the average value in the column.- Null values are not included in the average calculation.
AVG(DISTINCT column)
: Returns the average of distinct values in the column.
COUNT Function
COUNT(*)
: Returns the total number of rows in a table.COUNT(column)
: Returns the number of rows with non-NULL values in the specified column.COUNT(DISTINCT column)
: Returns the number of rows with unique, non-NULL values in the column.
MAX/MIN Functions
MAX(column)
: Returns the maximum non-null value in the column.MIN(column)
: Returns the minimum non-null value in the column.
SUM Function
SUM(column)
: Totals all values in the column.SUM(DISTINCT column)
: Sums only the distinct values in the column.
Counting Songs
- Shows how to use Aggregate Functions to calculate information about the data.
- Example is counting the number of songs with
SELECT COUNT(song_id) AS 'Number of Songs' FROM cd_makeup;
GROUP BY Clause
- Provides the ability to show the results of an aggregate function, grouped by a specific field.
- A
GROUP BY
clause is used when attributes outside of an aggregate function call are part of theSELECT
clause.
HAVING Clause
- The
HAVING
clause places restrictions according to the results of aggregate functions. - It is used instead of
WHERE
to limit results afterGROUP BY
is performed.
WHERE versus HAVING Clause
- A statement can have both a
WHERE
clause and aHAVING
clause - The
WHERE
clause applies to raw, ungrouped data and is evaluated before aGROUP BY
clause - The
HAVING
clause applies to grouped data and executes after aGROUP BY
ORDER BY Clause
- The
ORDER BY
clause sorts the rows returned - Order can be ascending (default) or descending.
- Multiple levels of sorting are possible.
SQL Review
- Expanded format of the
SELECT
statement: SELECT attribute(s) FROM tablename(s) WHERE condition(s) GROUP BY groupList HAVING haveCondition ORDER BY orderList;
WHERE
,GROUP BY
,HAVING
, andORDER BY
are all optional
Highly Abstract Tables
- Consider the song table from the cd_catalog database
- Includes SONG(song id, song title, length, artist id) and additional song attributes (tempo and mood)
- Highly abstract tables collapse common fields (Tempo, Mood, and Genre) into a tag/value relationship
- Create another, weak entity, that has an "attribute" field and a value field
- Benefit
- Very flexible
- Easy to handle unforeseen information
- Downside
- Queries get difficult
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.