Podcast
Questions and Answers
Match the SQL components with their roles:
Match the SQL components with their roles:
DML = Query and manipulate data DDL = Define database schema Transaction Control = Manage transaction boundaries Authorization = Specify access rights to data
Match the SQL data types with their descriptions:
Match the SQL data types with their descriptions:
char(n) = Fixed-length character string of length n varchar(n) = Variable-length character string with maximum length n int = Integer number numeric(p,d) = Fixed point number with precision p and d digits after decimal
Match the following SQL history events with their approximate dates:
Match the following SQL history events with their approximate dates:
IBM System R = 1970s SQL-86 = Mid 1980s SQL-92 = Early 1990s SQL:1999 = Late 1990s
Match the aspects of relation specification with their corresponding DDL function:
Match the aspects of relation specification with their corresponding DDL function:
Match each capability with the component of SQL that manages it:
Match each capability with the component of SQL that manages it:
Match the potential impact with the SQL feature that manages it:
Match the potential impact with the SQL feature that manages it:
Match the feature with an example of when it might be used:
Match the feature with an example of when it might be used:
Match the characteristic with the historic SQL standard that introduced it:
Match the characteristic with the historic SQL standard that introduced it:
Match the aggregate SQL functions with their descriptions:
Match the aggregate SQL functions with their descriptions:
Match the SQL clauses with their purposes in a query:
Match the SQL clauses with their purposes in a query:
Match the following subquery placements with their roles in a SQL statement:
Match the following subquery placements with their roles in a SQL statement:
Match the following SQL errors or constraints with their descriptions:
Match the following SQL errors or constraints with their descriptions:
Match the scenarios with the most appropriate SQL aggregate function or clause to use:
Match the scenarios with the most appropriate SQL aggregate function or clause to use:
Match the SQL clause with its corresponding function:
Match the SQL clause with its corresponding function:
Match the following operations with their corresponding SQL command aspect:
Match the following operations with their corresponding SQL command aspect:
Match the following modifications with their corresponding SQL operations:
Match the following modifications with their corresponding SQL operations:
Match the following SQL operations with the clause:
Match the following SQL operations with the clause:
Match each SQL concept with its description:
Match each SQL concept with its description:
Match the functions with the appropriate SELECT
clause expressions
Match the functions with the appropriate SELECT
clause expressions
Match the following keywords to their functions in SQL queries:
Match the following keywords to their functions in SQL queries:
Match the functionalities to the clauses typically used in SQL
SELECT
statements:
Match the functionalities to the clauses typically used in SQL
SELECT
statements:
Match the SQL string operation with its appropriate function:
Match the SQL string operation with its appropriate function:
Match the SQL ORDER BY
clause with its corresponding sorting order:
Match the SQL ORDER BY
clause with its corresponding sorting order:
Match the SQL set operation with its corresponding action:
Match the SQL set operation with its corresponding action:
Match the following components of a WHERE
clause with their correct function:
Match the following components of a WHERE
clause with their correct function:
Match the examples with their intended purpose:
Match the examples with their intended purpose:
Relate each SQL command to its use in set operations:
Relate each SQL command to its use in set operations:
Match each example with its equivalent description:
Match each example with its equivalent description:
Associate the below functions with the correct description:
Associate the below functions with the correct description:
Match the SQL clauses with their corresponding descriptions:
Match the SQL clauses with their corresponding descriptions:
Match the SQL queries with their intended outcomes regarding set operations on course_id
from the section
table:
Match the SQL queries with their intended outcomes regarding set operations on course_id
from the section
table:
Match the mathematical expressions with their SQL equivalents using the SOME
clause:
Match the mathematical expressions with their SQL equivalents using the SOME
clause:
Match the following scenarios with the appropriate SQL SOME
clause comparisons:
Match the following scenarios with the appropriate SQL SOME
clause comparisons:
Match SQL queries with their corresponding descriptions related to instructor salaries and departments:
Match SQL queries with their corresponding descriptions related to instructor salaries and departments:
Match each set comparison operator with its equivalent SQL clause:
Match each set comparison operator with its equivalent SQL clause:
Match the following SQL clauses with their appropriate use cases:
Match the following SQL clauses with their appropriate use cases:
Match the following SQL code snippets with their intended outcomes related to set comparison:
Match the following SQL code snippets with their intended outcomes related to set comparison:
Match the SQL clause with its function related to subqueries:
Match the SQL clause with its function related to subqueries:
Match the type of subquery with its characteristic:
Match the type of subquery with its characteristic:
Match the scenario with the appropriate SQL construct to use:
Match the scenario with the appropriate SQL construct to use:
Match the query objective with the appropriate SQL technique involving subqueries:
Match the query objective with the appropriate SQL technique involving subqueries:
Match each SQL statement with its equivalent description of functionality:
Match each SQL statement with its equivalent description of functionality:
Match each SQL operation with the database modification it performs:
Match each SQL operation with the database modification it performs:
Match each concept related to SQL subqueries with its equivalent description:
Match each concept related to SQL subqueries with its equivalent description:
Match each scenario with the most appropriate use-case for SQL subqueries:
Match each scenario with the most appropriate use-case for SQL subqueries:
Flashcards
SQL
SQL
A database language used for querying and managing data in relational database management systems (RDBMS).
SQL DDL
SQL DDL
Part of SQL used to define the structure of the database, including creating, altering, and deleting tables.
SQL DML
SQL DML
Part of SQL used for retrieving, inserting, updating, and deleting data within the database tables.
Integrity Constraints
Integrity Constraints
Signup and view all the flashcards
Relation Schema
Relation Schema
Signup and view all the flashcards
char(n)
char(n)
Signup and view all the flashcards
varchar(n)
varchar(n)
Signup and view all the flashcards
numeric(p, d)
numeric(p, d)
Signup and view all the flashcards
COUNT(DISTINCT)
COUNT(DISTINCT)
Signup and view all the flashcards
COUNT(*)
COUNT(*)
Signup and view all the flashcards
GROUP BY
GROUP BY
Signup and view all the flashcards
HAVING clause
HAVING clause
Signup and view all the flashcards
Nested Subquery
Nested Subquery
Signup and view all the flashcards
Adding a new attribute
Adding a new attribute
Signup and view all the flashcards
Dropping an attribute
Dropping an attribute
Signup and view all the flashcards
SELECT Clause
SELECT Clause
Signup and view all the flashcards
FROM Clause
FROM Clause
Signup and view all the flashcards
WHERE Clause
WHERE Clause
Signup and view all the flashcards
SELECT DISTINCT
SELECT DISTINCT
Signup and view all the flashcards
SELECT *
SELECT *
Signup and view all the flashcards
AS Clause
AS Clause
Signup and view all the flashcards
LIKE '%substring%'
LIKE '%substring%'
Signup and view all the flashcards
Escape Character in LIKE
Escape Character in LIKE
Signup and view all the flashcards
ORDER BY
ORDER BY
Signup and view all the flashcards
BETWEEN
BETWEEN
Signup and view all the flashcards
UNION
UNION
Signup and view all the flashcards
INTERSECT
INTERSECT
Signup and view all the flashcards
EXCEPT
EXCEPT
Signup and view all the flashcards
UNION ALL, INTERSECT ALL, EXCEPT ALL
UNION ALL, INTERSECT ALL, EXCEPT ALL
Signup and view all the flashcards
Query for courses offered at most once in 2017
Query for courses offered at most once in 2017
Signup and view all the flashcards
Subqueries in FROM clause?
Subqueries in FROM clause?
Signup and view all the flashcards
WITH Clause
WITH Clause
Signup and view all the flashcards
Scalar Subquery
Scalar Subquery
Signup and view all the flashcards
Deletion of Tuples
Deletion of Tuples
Signup and view all the flashcards
Finding Departments with High Average Salaries
Finding Departments with High Average Salaries
Signup and view all the flashcards
Benefits of WITH clause
Benefits of WITH clause
Signup and view all the flashcards
Listing Departments with Instructor Count
Listing Departments with Instructor Count
Signup and view all the flashcards
Set Membership with 'in'
Set Membership with 'in'
Signup and view all the flashcards
Set Exclusion with 'not in'
Set Exclusion with 'not in'
Signup and view all the flashcards
'not in' with literal values
'not in' with literal values
Signup and view all the flashcards
Counting Distinct IDs
Counting Distinct IDs
Signup and view all the flashcards
'> some' Clause
'> some' Clause
Signup and view all the flashcards
Definition of 'some' Clause
Definition of 'some' Clause
Signup and view all the flashcards
Finding Maximum Scalar Value
Finding Maximum Scalar Value
Signup and view all the flashcards
Definition of 'all' Clause
Definition of 'all' Clause
Signup and view all the flashcards
Study Notes
Introduction to SQL
- Chapter 3 introduces database system concepts
- Focus on key relational database language: SQL
History of SQL
- IBM originally developed Sequel as part of System R project
- Sequel became Structured Query Language (SQL)
- ANSI and ISO created standard SQL versions, including SQL-86, SQL-89, SQL-92, SQL:1999, and SQL:2003
- Commercial database systems typically offer SQL-92 features along with extensions
Structure of SQL
- SQL includes DML for querying and modifying data
- Integrity constraints are specified using DDL commands
- View definitions use DDL commands
- Transaction control manages transaction beginnings and endings
- Embedded SQL and dynamic SQL statements utilize general-purpose programming languages
- Authorization manages access rights to relations and views.
DDL (Data Definition Language)
- DDL is used to specify information about relations:
- Schemas
- Value types for each attribute
- Integrity constraints
- Indices
- Security and authorization
- Physical storage structure on disk
Domain Types in SQL
char(n)
: Fixed-length character string with length nvarchar(n)
: Variable-length character string with maximum length nint
: Integer, machine-dependentsmallint
: Small integer, machine-dependentnumeric(p,d)
: Fixed-point number with precision of p digits and d digits to the right of the decimal pointreal
,double precision
: Floating-point numbers with machine-dependent precisionfloat(n)
: Floating-point number with user-specified precision of at least n digits
Create Table Construct
create table r (A1 D1, A2 D2, ..., An Dn, (integrity-constraint1), ..., (integrity-constraintk))
r
is the relation nameAi
is an attribute name in the schema of relation rDi
is the data type of values in the domain of attributeAi
- Example:
create table instructor (ID char(5), name varchar(20), dept_name varchar(20), salary numeric(8,2))
Integrity Constraints in Create Table
- Primary key
primary key (A1, ..., An)
- Foreign key
foreign key (Am, ..., An) references r
- Not null
not null
- SQL prevents updates that violate integrity constraints
- Example:
create table instructor (ID char(5), name varchar(20) not null, dept_name varchar(20), salary numeric(8,2), primary key (ID), foreign key (dept_name) references department)
Updating Tables
- Insertion:
insert into instructor values ('10211', 'Smith', 'Biology', 66000)
- Deletion:
delete from student
- Drop Table:
drop table r
- Alter Table:
alter table r add A D
(adds attribute A with domain D to relation r)
Basic SQL Query Structure
select A1, A2, ..., An
from relation name(s),r1, r2, ..., rm
whereP
A
represents an attributeR
represents a relationP
is a predicate- Result of an SQL query is a relation
The Select Clause
select
clause lists attributes in query result- Equivalent to projection operation in relational algebra
- Example:
select name from instructor
- SQL names are case-insensitive
distinct
is used afterselect
to remove duplicatesall
specifies duplicates should not be removed- Asterisk (*) in
select
clause denotes "all attributes"
Literals in the Select Clause
- An attribute can be a literal when there's no
from
clause - A table with one column will show the value of the select
- A name can be given to the column using the as function
- Example:
select '437' as FOO
- can be used with a
from
clause - It will generate table with one column and N rows (based on the number of rows in the table) each row as the value
- Example
select 'A' from instructor
- Result: is a table with one column and N rows (number of tuples in the instructors table), each row with value “A”
Arithmetic Expressions in Select Clause
- The
select
clause may contain arithmetic expressions like +, -, *, / - Can rename using
"as"
clause - Example:
select ID, name, salary/12 as monthly_salary
The Where Clause
where
clause specifies query conditions- Corresponds to the selection predicate of the relational algebra
- Example:
select name from instructor where dept_name = 'Comp. Sci.'
- Logical connectives are
and, or, not
. - Comparison operators are
<, <=, >, >=, =, <>
The From Clause
- Lists from which to select relations of the query
- Corresponds to the Cartesian product operation of the relational algebra
- Example:
select * from instructor, teaches
(generates every possible instructor-teaches pair) - Attributes are renamed for common attributes, like
instructor.ID
Rename Operation
- The
as
clause is used in SQL to rename relations and attributes - Format =
old-name as new-name
- The keyword as is optional
- Example finding instructors with salary > some instructor in 'Comp. Sci':
select distinct T.name from instructor as T, instructor as S where T.salary > S.salary and S.dept_name = 'Comp. Sci.'
String Operations
like
operator matches strings, using patterns%
character matches any substring_
character matches any single character- Example:
select name from instructor where name like '%dar%'
- String matching is case sensitive.
- Concatenation uses
"||"
- You can convert case and find string length
Ordering the Display of Tuples
- Use
order by
clause to list alphabetically - Specify
desc
for descending orasc
ascending order. - Example sorting:
select distinct name from instructor order by name
Where Clause Predicates
between
comparison operator is used to find values within a range- Example:
select name from instructor where salary between 90000 and 100000
Set Operations
union
returns combined results filtering for unique entriesintersect
returns the values that the queries have in commonexcept
returns value of the first query that the second query does not have- Example of extracting courses, combining queries for fall and spring semesters,
(select course_id from section where sem = 'Fall' and year = 2017) union (select course_id from section where sem = 'Spring' and year = 2018)
union, intersect, except
eliminate duplicates by default- use
union all, intersect all, except all
to keep all duplicates
Null Values
- Tuples can have a null value in attributes
null
means a value is unknown or doesn't exist- Any calculation with
null
results isnull
is null
checks for null values:select name from instructor where salary is null
is not null
returns success if value is not null
Null Value Comparison
- SQL considers any comparison with a null value as unknown, except for
is null
andis not null
- Expressions involving
null
result inunknown
value that must also be handled - Result of where clause is treated as
false
if evaluates to unknown
Aggregate Functions
- Used to operate on a multiset of values in a column of a relation, returning one single value
avg
: Average valuemin
: Minimum valuemax
: Maximum valuesum
: Sum of valuescount
: Number of values- Example showing avg of instructors,
select avg (salary) from instructor where dept_name= 'Comp. Sci.'
Aggregate Functions with Group By
- Attributes outside aggregate functions must appear in
group by
list - Predicates in having clause are applied after forming groups versus predicates in
where
, applied before forming groups - Example:
select dept_name, avg (salary) as avg_salary from instructor group by dept_name having avg (salary) > 42000
Nested Subqueries
- SQL can nest subqueries
- A subquery is a
select-from-where
expression nested within another query
Nested Subqueries Location
From clause
: Any valid subquery can replacer₁
,r₂
, ...,rₘ
Where clause
: Expressions can replaceP
:B SomeOperation (subquery)
Select clause
:Aᵢ
can be replaced a query that calculates a value
Set Membership Operations
- Useful queries involve membership operations involving the keyword
in
- Example to find unique IDs:
select distinct course_id from section where semester = 'Fall' and year= 2017 and course_id in (select course_id from section where semester = 'Spring' and year= 2018)
Set Comparison Operations
- SQL includes two set comparison operators that include all and some
Some Clause
- Use syntax
> some
- Example:
where salary > some (select salary from instructor where dept name = 'Biology')
All Clause
- Use syntax
> all
where the salary is greate than anything - Example:
where salary > all (select salary from instructor where dept name = 'Biology')
Test for Empty Relations
exists
checks for non-empty subquery return valuer ≠ Ø
not exists
means empty subqueryr = Ø
Use of exists Clause
- Example:
select course_id from section as S where semester = 'Fall' and year = 2017 and exists (select * from section as T where semester = 'Spring' and year= 2018 and S.course_id = T.course_id)
Test for Absence of Duplicate Tuples
unique
tests for subquery duplicate tuples- Evaluates to true if subquery contains no duplicate tuples
- Example checking absence of duplicate tuples:
select T.course_id from course as T where unique (select R.course_id from section as R where T.course_id = R.course_id and R.year = 2017)
Subqueries in the From Clause
- Can be used as expressions
- Example:
select dept_name, avg_salary from (select dept_name, avg (salary) as avg_salary from instructor group by dept_name) where avg_salary > 42000
With Clause
- Provides a way to define a temporary relation, definition available only to the query where the with clause happens
- Example:
with max_budget (value) as (select max(budget) from department) select department.name from department, max_budget where department.budget = max_budget.value
Complex Queries using with Clause
- Example:
with dept_total (dept_name, value) as (select dept_name, sum(salary) from instructor group by dept_name), dept_total_avg(value) as (select avg(value) from dept_total) select dept_name from dept_total, dept_total_avg where dept_total.value > dept_total_avg.value
Scalar Subquery
- Used where a single value is expected
- Example to display all departments:
select dept_name, (select count(*) from instructor where department.dept_name = instructor.dept_name) as num_instructors from department
Modification of the Database
- Includes deletion of tuples, insertion of new tuples and updating of existing values
Deletion
- Deletes all instructors:
delete from instructor
- Example deleting instructor:
delete from instructor where dept name in (select dept name from department where building = 'Watson')
- Example delete all instructots where their salary is lower than the average
delete from instructor where salary < (select avg (salary) from instructor)
Insertion
- Example adding a new tuple:
insert into course values ('CS-437', 'Database Systems', 'Comp. Sci.', 4)
- Make each student in the Music department who has earned more than 144 credit hours an instructor in the Music department with a salary of $18,000:
insert into instructor select ID, name, dept name, 18000 from student where dept_name = 'Music' and total_cred > 144
Updates
- Example raise by 5%:
update instructor set salary = salary * 1.05
- Example 2 if less than 70000:
update instructor set salary = salary* 1.05 where salary <= 100000
- Can use case statements for conditionals, if they are the same type
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Test your knowledge of SQL with this matching game. Match SQL components with their roles, data types with descriptions, and more. Enhance your understanding of SQL clauses, functions, and error handling.