Basic Retrieval Queries in SQL
Basic Retrieval Queries in SQL
Retrieving data is the primary purpose of a database system, and SQL provides a powerful and flexible mechanism for doing so using the SELECT statement. Unlike relational algebra, which is procedural, SQL is a declarative language: the user specifies what data is required, not how to retrieve it.
SQL vs Relational Model (Important Conceptual Difference)
-
Duplicate rows are allowed
-
Duplicate results are not eliminated automatically
-
Duplicate elimination must be explicitly requested
This distinction is crucial when interpreting SQL query results.
SELECT–FROM–WHERE Structure of Basic SQL Queries
The fundamental structure of an SQL retrieval query is:
Clause Functions
| Clause | Purpose |
|---|---|
| SELECT | Specifies attributes to display (projection) |
| FROM | Specifies tables needed for the query |
| WHERE | Specifies selection and join conditions |
Query Processing Conceptually
-
The DBMS forms a Cartesian product of tables in the FROM clause
-
The WHERE clause filters tuples (selection + join)
-
The SELECT clause projects required attributes
Example 1: Simple Selection and Projection
Retrieve birth date and address of employee named John B. Smith
-
Uses only one table
-
WHERE clause filters tuples
-
SELECT clause chooses attributes
Equivalent to:
-
Selection + Projection in relational algebra
Example 2: Join Query
Retrieve employees working in the ‘Research’ department
Types of conditions:
-
Dname = 'Research'→ selection condition -
Dnumber = Dno→ join condition
This is a select–project–join query.
Example 3: Multiple Joins
-
Multiple tables joined
-
Each result tuple combines:
-
A project
-
Its controlling department
-
The department manager
-
Ambiguous Attribute Names, Aliasing, and Tuple Variables
Ambiguous Attribute Names
When multiple tables contain attributes with the same name, attributes must be qualified:
This prevents ambiguity.
Aliasing Tables (Tuple Variables)
Aliases simplify queries and are mandatory when:
-
The same table appears more than once in a query
Example: Self-join
-
E: employee (subordinate) -
S: supervisor -
This is a recursive (self-join) query
Aliases:
-
Improve readability
-
Enable self-joins
-
Are strongly recommended
Missing WHERE Clause and Use of Asterisk (*)
Missing WHERE Clause
-
Single table → all rows selected
-
Multiple tables → Cartesian product
⚠️ Danger: forgetting join conditions leads to huge, incorrect results.
Asterisk (*) Usage
-
Retrieves all attributes
-
Can be table-qualified:
Tables as Sets vs Multisets in SQL
Duplicate Tuples in SQL
SQL does not remove duplicates by default because:
-
Duplicate elimination is expensive
-
Users may want duplicates
-
Aggregate functions often need duplicates
DISTINCT Keyword
-
Removes duplicate rows
-
Without DISTINCT → behaves as
SELECT ALL
Set Operations in SQL
| Operation | Description |
|---|---|
| UNION | Combines results, removes duplicates |
| EXCEPT | Set difference |
| INTERSECT | Common tuples |
Conditions:
-
Relations must be type compatible
-
Same number and order of attributes
Example (UNION):
Multiset versions:
-
UNION ALL -
EXCEPT ALL -
INTERSECT ALL
Pattern Matching and Arithmetic Operators
LIKE Operator (Substring Matching)
Wildcards:
-
%→ zero or more characters -
_→ exactly one character
Example:
Escaping Wildcards
Arithmetic Operators
| Operator | Meaning |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
Example (salary increase):
BETWEEN Operator
Equivalent to:
6.3.6 Ordering Query Results
SQL results are unordered by default.
ORDER BY Clause
-
Default order: ascending (ASC)
-
Use
DESCfor descending order
Summary of Basic SQL Retrieval Queries
General Query Format
Key Takeaways
-
SQL is declarative
-
SELECT = projection
-
WHERE = selection + join
-
FROM = involved tables
-
SQL supports:
-
Joins
-
Set operations
-
Pattern matching
-
Arithmetic expressions
-
Ordering of results
-
Comments
Post a Comment