Simple Queries in SQL
Simple Queries in SQL
- Introduction to the basic
SELECT-FROM-WHEREquery structure, which is analogous to a selection in relational algebra. - Performing projection by listing specific attributes in the
SELECTclause instead of using*. - Renaming columns in the query result using the
ASkeyword, also known as aliasing. - Using expressions, calculations, and constants in the
SELECTclause, similar to an extended projection. - Constructing selection conditions in the
WHEREclause using comparison operators (=,<>,<,>,<=,>=) and logical operators (AND,OR,NOT). - Understanding the relationship between simple SQL queries and the relational algebra expression \(\pi_L(\sigma_C(R))\)
- Comparing strings based on lexicographical (dictionary) order.
- Matching strings to patterns using the
LIKEoperator with wildcard characters%(any sequence of zero or more characters) and_(any single character). - Using the
ESCAPEclause to search for the literal%and_characters within aLIKEpattern. - Representing and comparing date and time values using the
DATE,TIME, andTIMESTAMPdata types and their standard formats. - Understanding the concept and interpretations of
NULLvalues (e.g., value unknown, inapplicable, or withheld). - Rules for handling
NULLin expressions: arithmetic operations withNULLresult inNULL, and comparisons withNULLresult in the truth-valueUNKNOWN. - Checking for nullity using the
IS NULLandIS NOT NULLpredicates. - Introduction to three-valued logic (TRUE, FALSE, UNKNOWN) and its application to logical operators
AND,OR, andNOT. - The
WHEREclause filtering mechanism, which only includes tuples for which the condition evaluates toTRUE, discarding those that areFALSEorUNKNOWN. - Sorting query results using the
ORDER BYclause, with options for ascending (ASC) or descending (DESC) order on one or more attributes. - Using expressions in the
ORDER BYclause to sort based on calculated values.
#1
read: Section 6.1: Simple Queries in SQL (section)
model: pro
rules: bullet definition math presentation short
Present the following. Use definition formal definition when it applies.
- Introduction to the basic `SELECT-FROM-WHERE` query structure, which is analogous to a selection in relational algebra.
- Performing projection by listing specific attributes in the `SELECT` clause instead of using `*`.
- Renaming columns in the query result using the `AS` keyword, also known as aliasing.
- Using expressions, calculations, and constants in the `SELECT` clause, similar to an extended projection.
- Constructing selection conditions in the `WHERE` clause using comparison operators (`=`, `<>`, `<`, `>`, `<=`, `>=`) and logical operators (`AND`, `OR`, `NOT`).
- Understanding the relationship between simple SQL queries and the relational algebra expression $\pi_L(\sigma_C(R))$
- Comparing strings based on lexicographical (dictionary) order.
- Matching strings to patterns using the `LIKE` operator with wildcard characters `%` (any sequence of zero or more characters) and `_` (any single character).
- Using the `ESCAPE` clause to search for the literal `%` and `_` characters within a `LIKE` pattern.
- Representing and comparing date and time values using the `DATE`, `TIME`, and `TIMESTAMP` data types and their standard formats.
- Understanding the concept and interpretations of `NULL` values (e.g., value unknown, inapplicable, or withheld).
- Rules for handling `NULL` in expressions: arithmetic operations with `NULL` result in `NULL`, and comparisons with `NULL` result in the truth-value `UNKNOWN`.
- Checking for nullity using the `IS NULL` and `IS NOT NULL` predicates.
- Introduction to three-valued logic (TRUE, FALSE, UNKNOWN) and its application to logical operators `AND`, `OR`, and `NOT`.
- The `WHERE` clause filtering mechanism, which only includes tuples for which the condition evaluates to `TRUE`, discarding those that are `FALSE` or `UNKNOWN`.
- Sorting query results using the `ORDER BY` clause, with options for ascending (`ASC`) or descending (`DESC`) order on one or more attributes.
- Using expressions in the `ORDER BY` clause to sort based on calculated values.
6.1 Simple Queries in SQL
The
SELECT-FROM-WHEREStatementThe most basic form of a query in SQL consists of three clauses:
SELECT,FROM, andWHERE. This structure is analogous to the selection operation of relational algebra.SELECTlists the attributes to be displayed in the result.FROMspecifies the relation from which to retrieve tuples.WHEREprovides a condition that tuples must satisfy to be included in the result.
A simple SQL query of the form:
SELECT L
FROM R
WHERE C;where \(L\) is a list of expressions, \(R\) is a relation, and \(C\) is a condition, is equivalent to the relational-algebra expression:
\[ \pi_L(\sigma_C(R)) \]
The query first applies the selection condition \(C\) to the relation \(R\) and then projects the result onto the attributes and expressions specified in list \(L\).
Projection in SQL
To project a relation onto a subset of its attributes, list the desired attributes in the
SELECTclause. The wildcard*is used to select all attributes.-- Selects only the title and length from the Movies relation SELECT title, length FROM Movies WHERE studioName = 'Disney' AND year = 1990;Renaming Attributes and Expressions
The
ASkeyword allows for the renaming of an attribute or an expression in the output relation. The keywordASis optional.SELECT title AS name, length AS duration FROM Movies;Expressions in the
SELECTClauseThe
SELECTclause can include expressions, which are evaluated for each selected tuple. This functionality is analogous to extended projection in relational algebra. Constants may also be included as expressions.-- Calculates length in hours and includes a constant 'hrs.' column SELECT title, length/60.0 AS lengthInHours, 'hrs.' AS units FROM Movies;Conditions in the
WHEREClauseThe
WHEREclause filters tuples using a condition.- Comparison Operators:
=,<>(not equal),<,>,<=,>=. - Logical Operators:
AND,OR,NOT.ANDhas higher precedence thanOR. Parentheses may be used to clarify the order of evaluation.
SELECT title FROM Movies WHERE (year > 1970 OR length < 90) AND studioName = 'MGM';- Comparison Operators:
String Operations
- Comparison: Strings are compared lexicographically (i.e., dictionary order).
'fodder'is less than'foo'. A string is less than another if it is a proper prefix, e.g.,'bar'is less than'bargain'. - Pattern Matching: The
LIKEoperator is used for matching strings to patterns.%matches any sequence of zero or more characters._matches any single character.
-- Finds movies with a title containing 's WHERE title LIKE '%''s%';- Escape Characters: The
ESCAPEclause allows for searching for literal%or_characters.
-- Matches a string that starts and ends with a literal '%' WHERE name LIKE 'x%%x%' ESCAPE 'x';- Comparison: Strings are compared lexicographically (i.e., dictionary order).
Date and Time Types
SQL provides special data types for dates and times, which can be compared.
DATE:DATE 'YYYY-MM-DD'TIME:TIME 'HH:MI:SS'TIMESTAMP:TIMESTAMP 'YYYY-MM-DD HH:MI:SS'
Null Values
SQL uses the special value
NULLto represent missing information.NULLis not a value in the conventional sense. Its interpretation can be:- Value Unknown: A value exists but is not known.
- Value Inapplicable: No meaningful value exists for the attribute.
- Value Withheld: The value is not available to the user.
Expressions and Comparisons with
NULLSpecial rules govern operations involving
NULL.- An arithmetic expression involving
NULLevaluates toNULL. For example, ifxisNULL, thenx + 5isNULL. - A comparison involving
NULLevaluates to the special truth-valueUNKNOWN. For example, ifxisNULL, thenx = 5isUNKNOWN. - To test if a value is
NULL, use theIS NULLorIS NOT NULLpredicate. The expressionx = NULLis not valid for this purpose.
- An arithmetic expression involving
Three-Valued Logic
The presence of
UNKNOWNrequires a three-valued logic system. The logical operatorsAND,OR, andNOTare extended as follows, whereTRUE=1,UNKNOWN=1/2, andFALSE=0.x AND y=min(x, y)x OR y=max(x, y)NOT x=1 - x
A
WHEREclause only accepts tuples for which the condition evaluates toTRUE. Tuples for which the condition isFALSEorUNKNOWNare rejected.Ordering the Output
The
ORDER BYclause sorts the tuples of the result.- Sorting can be on one or more attributes. Subsequent attributes break ties from previous ones.
ASCspecifies ascending order (the default).DESCspecifies descending order.- The
ORDER BYclause can also contain expressions.
SELECT * FROM Movies WHERE studioName = 'Disney' AND year = 1990 ORDER BY length DESC, title;
- Version: 1
- Model: gemini-2.5-pro
- Created: 2025-09-20 12:50:27
- Duration: 30.130687952041626
- Cummulative cost: $4.12e-02