Database System Basic - 1

Intermediate SQL


Example Database Table

student (sid, name, login, gpa)

SidNameLoginAgeGpa
53666Kanyekanye@cs444.0
53688Bieberjbieber@cs273.9
53655Tupacshakur@cs253.5

enrolled (sid, cid, grade)

SidCidGrade
5366615-445C
5368815-721A
5368815-826B
5365515-445B
5366615-721C

course (cid, name)

CidName
15-445Database Systems
15-721Advanced Database Systems
15-826Data Mining
15-823Advanced Topics in Databases

 

(P.S. The SQL below all use example data table)

Basic Syntax

Basic

Ex: Which students age > 25?

Joins

Ex: Which students got an A in 15-721?

 

A. Aggregates

NameDefinition
AVG(col)Return the average col value
MIN(col)Return the minimum col value
MAX(col)Return the maximum col value
SUM(col)Return the sum of values in col
COUNT(col)Return # of values for col

Ex: Get # of students with a "@cs" login?

 

Multiple Aggregates

 

Distinct Aggregates

 

Example:

Get the average GPA of students enrolled in each course?

 

B. Group By

Project tuples into subsets and calculate aggregates against each subset.

Display:

image-20220514185653747

Non-aggregated values in SELECT output clause must appear in GROUP BY clause. For example:

 

C. Having

Filters results based on aggregation computation. Like a WHERE clause for a GROUP BY.

Display:

image-20220514190528979

 

D. String Operations

 String CaseString Quotes
SQL-92SensitiveSingle Only
PostgresInsensitiveSingle Only
MySQLSensitiveSingle / Double
SQLiteSensitiveSingle / Double
DB2SensitiveSingle Only
OracleSensitiveSingle Only

SQL-92:

MySQL:

 

1. LIKE Operators

LIKE: is used for string matching.

String-matching Operators

'%' --> Matches any substring (include empty strings)

'_' --> Match any one character

 

2. || Operator (use in SQL-92)

But in MySQL use CONCAT()

 

E. Date / Time Operations

SQL-92 & MySQL:

...

 

F. Output Redirection

1. Store

Store query results in another table:

SQL-92:

MySQL:

 

2. Insert

Insert tuples from query into another table:

--> Inner SELECT must generate the same columns as the target table

SQL-92:

 

G. Output Control

1. ORDER BY <column*> [ASC | DESC]

--> Order the output tuples by the values in one or more of their columns.

 

#3 Display ==> image-20220514193957999

 

2. LIMIT [offset]

--> Limit the # of tuples return in output

--> Can set an offset to return a "range"

 

H. Nested Queries

Queries containing other queries.

Inner queries can appear anywhere in query.

inside ( ): Inner Query

outside ( ): Outer Query

 

Ex. Get the names of students in '15-445'

 

Nested QueriesDefinition
ALLMust satisfy expression for all rows in the sub-query
ANYMust satisfy expression for at least one row in the sub-query
INEquivalent to ' = ANY( ) '
EXISTSAt least one row is returned

 

NOT EXISTS

Ex. Find all courses that have no students enrolled in it.

Display ==> image-20220514195713633

 

I. Window Functions

1. Special window functions:

--> ROW_NUMBER( ) -> # of the current row

--> RANK( ) -> Order position of the current row

 

2. OVER Keyword

The OVER keyword specifies how to group togther tuples when computing the window function.

Use PATITION BY to specify group. (also can use ORDER BY)

 

Ex. Find the student with the second highest grade for each course.

 

J. Common Table Expressions

Alternative to nested queries and views.

 

Ex. Find student record with the highest id that is enrolled in at least one course.

 

K. CTE - Recursion

Ex. Print the sequence of numbers from 1 to 10