Databases and Data Modeling: SQL Basics

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Listen to an AI-generated conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

SQL is based on relational algebra.

True (A)

Which SQL version introduced outer joins?

  • SQL99
  • SQL89
  • SQL2003
  • SQL92 (correct)

What clause is required for outer joins introduced by SQL92?

ON

The words 'INNER' and '______' are optional in SQL join statements.

<p>OUTER</p>
Signup and view all the answers

Match the SQL function with its description:

<p>LOWER() = Converts a string to lower-case letters. UPPER() = Converts a string to upper-case letters. LENGTH() = Returns the number of characters in a string. CONCAT() = Appends one string to another.</p>
Signup and view all the answers

Which function returns the starting position of a substring within a string?

<p>INSTR() (A)</p>
Signup and view all the answers

The function ________ takes a string, a starting position, and a length, and returns a substring.

<p>SUBSTR</p>
Signup and view all the answers

The concatenation operator || is supported in MySQL.

<p>False (B)</p>
Signup and view all the answers

Which function truncates a number after a specified number of decimal places?

<p>TRUNCATE() (B)</p>
Signup and view all the answers

Which function returns the remainder of one number divided by another?

<p>MOD</p>
Signup and view all the answers

Multi-row functions are also known as aggregate functions.

<p>True (A)</p>
Signup and view all the answers

Which aggregate function calculates the average value in a column, ignoring NULL values?

<p>AVG() (C)</p>
Signup and view all the answers

The ________ function returns the number of rows in a table.

<p>COUNT(*)</p>
Signup and view all the answers

What do the MAX() and MIN() functions return?

<p>non-null value</p>
Signup and view all the answers

The SUM function can only be used on numeric columns.

<p>True (A)</p>
Signup and view all the answers

Which SQL clause is used to group rows that have the same values in specified columns into summary rows?

<p>GROUP BY (D)</p>
Signup and view all the answers

When using a GROUP BY clause, should all attributes that are not part of the aggregate function be included?

<p>yes</p>
Signup and view all the answers

The ________ clause is used to filter results after grouping has been performed.

<p>HAVING</p>
Signup and view all the answers

The WHERE clause filters data after the GROUP BY clause is applied.

<p>False (B)</p>
Signup and view all the answers

Which clause is used to sort the rows?

<p>ORDER BY (B)</p>
Signup and view all the answers

The SQL92 standard has exactly the same support for JOIN types as SQL89.

<p>False (B)</p>
Signup and view all the answers

In SQL, what function is used to return a string with all lowercase letters?

<p>LOWER()</p>
Signup and view all the answers

In SQL, which of these aggregate functions returns the total of all values in a given column?

<p>SUM() (B)</p>
Signup and view all the answers

In SQL, the ________ keyword is used to eliminate duplicate values in an aggregate function.

<p>DISTINCT</p>
Signup and view all the answers

In SQL, other clauses can accomplish sorting, so the ORDER BY is not that important.

<p>False (B)</p>
Signup and view all the answers

What is a potential benefit of using highly abstract tables?

<p>Very flexible and easy to handle unforeseen information (D)</p>
Signup and view all the answers

What symbol is used for the concatenation operator in SQL?

<p>||</p>
Signup and view all the answers

When joining tables and the attributes used are named the same, can use ____________. (SQL 92 only)

<p>USING()</p>
Signup and view all the answers

The UNIQUE constraint enforces that all values in a column are different.

<p>True (A)</p>
Signup and view all the answers

What is a downside of highly abstract tables?

<p>Queries get difficult (B)</p>
Signup and view all the answers

Match the following SQL commands with their function.

<p>SELECT = Chooses which data to retrieve FROM = Specifies the table(s) to retrieve data from WHERE = Filters the data to be shown GROUP BY = Aggregates data according to specified columns</p>
Signup and view all the answers

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?

<p>Considering all the required tables (B)</p>
Signup and view all the answers

When doing multi-table joins in SQL, it is generally more efficient to join all tables simultaneously rather than joining two at a time.

<p>False (B)</p>
Signup and view all the answers

In SQL, what function is used to extract a substring from a string, given a starting position and the number of characters to extract?

<p>SUBSTR</p>
Signup and view all the answers

In SQL, the ______ function transforms a string to uppercase.

<p>UPPER()</p>
Signup and view all the answers

In SQL, SQL statements are case sensitive and must be typed in the correct case for it to be run.

<p>False (B)</p>
Signup and view all the answers

Which SQL clause is used to limit the records before a GROUP BY is performed?

<p>WHERE (A)</p>
Signup and view all the answers

In SQL, what does the term INNER JOIN refer to?

<p>shared records</p>
Signup and view all the answers

In SQL, the keyword ________ indicates that every value in a given column has its own value.

<p>UNIQUE</p>
Signup and view all the answers

In multi-table joins, it is okay for multiple records to share a common value.

<p>False (B)</p>
Signup and view all the answers

What is the correct order of operations to run the code properly?

<p>SELECT-&gt;FROM-&gt;WHERE-&gt;GROUP BY-&gt;HAVING-&gt;ORDER BY (C)</p>
Signup and view all the answers

Flashcards

UNIQUE constraint

A constraint ensuring all values in a column are unique.

INNER and OUTER

Words 'INNER' and 'OUTER' are optional in SQL joins.

Outer Joins

Introduced with SQL92, requires ON clause.

Inner Joins

Available since SQL89, ON clause not required.

Signup and view all the flashcards

Multi-table Joins

Joins multiple tables.

Signup and view all the flashcards

Building Multi-table Joins

Multi-table joins

Signup and view all the flashcards

SQL92

SQL92 standard supports all types of joins.

Signup and view all the flashcards

SQL89

SQL89 standard supports only INNER JOINS.

Signup and view all the flashcards

LOWER() function

SQL function transforming text to lowercase.

Signup and view all the flashcards

UPPER() function

SQL function transforming text to uppercase.

Signup and view all the flashcards

LENGTH() function

SQL function returning the length of a string.

Signup and view all the flashcards

INSTR() function

SQL function finding a substring's starting position.

Signup and view all the flashcards

SUBSTR() function

SQL function extracting a substring.

Signup and view all the flashcards

CONCAT() function

SQL function concatenating two strings.

Signup and view all the flashcards

Concatenation Operator (||)

A concatenation operator in SQL.

Signup and view all the flashcards

ROUND(n,m) function

SQL function rounding a number to m decimal places.

Signup and view all the flashcards

TRUNCATE(n,m) function

SQL function truncating a number after m decimal places.

Signup and view all the flashcards

MOD(n,m) function

SQL function returning the remainder of n divided by m.

Signup and view all the flashcards

Multi-Row Functions

Aggregate functions that operate on a column of data.

Signup and view all the flashcards

AVG() function

SQL function returning the average value in a column.

Signup and view all the flashcards

COUNT() function

SQL function returns the number of rows.

Signup and view all the flashcards

MAX()/MIN() functions

SQL functions return max/min values.

Signup and view all the flashcards

SUM() function

SQL function returning the sum of values in a column.

Signup and view all the flashcards

GROUP BY clause

Clauses that provide the GROUP BY clause.

Signup and view all the flashcards

HAVING clause

SQL clause is used with aggregate functions.

Signup and view all the flashcards

ORDER BY clause

SQL clause sorts rows in a result set.

Signup and view all the flashcards

Highly Abstract Tables

Collapses common fields into a tag/value relationship.

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 string s in all lowercase.
  • UPPER(s): Returns string s 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 string s.
  • Very useful when doing string comparisons

INSTR Function

  • INSTR(s1,s2) (or LOCATE(s2,s1) in MySQL) checks if string s2 is in string s1.
  • Returns the start position of the first match; returns 0 if s2 is not in s1.

SUBSTR Function

  • SUBSTR(s,n,m): Returns a substring of s starting at position n and of length m.

CONCAT Function

  • CONCAT(s1,s2): Appends string s2 to string s1.
  • MySQL allows CONCAT overloads

Concatenation Operator

  • || is a SQL concatenation operator.
  • Not supported in MySQL

ROUND/TRUNCATE Functions

  • ROUND(n,m): Rounds number n to m decimal places.
  • TRUNCATE(n,m): Truncates number n after m decimal places.

MOD Function

  • MOD(n,m): Returns the remainder when n is divided by m.

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(), and SUM()

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 the SELECT clause.

HAVING Clause

  • The HAVING clause places restrictions according to the results of aggregate functions.
  • It is used instead of WHERE to limit results after GROUP BY is performed.

WHERE versus HAVING Clause

  • A statement can have both a WHERE clause and a HAVING clause
  • The WHERE clause applies to raw, ungrouped data and is evaluated before a GROUP BY clause
  • The HAVING clause applies to grouped data and executes after a GROUP 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, and ORDER 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.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser