Exam 3 Flashcards

1
Q

SQL original name

A

SEQUEL: Structured English Query Language: Part of System R in 1973

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

SQL stands for

A

Structured Query Language

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

SQL is based on

A

Based on relational tuple calculus and some algebra

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

ANSI & ISO Standards for SQL

A

SQL/86, SQL 89, 92, SQL2, SQL3, Revisions in 2003, 2006, 2008, 2011…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

SQL is supported by

A

IBM DB2, ORACLE, SYSBASE, SqlServer, MySQL, many more (sort of all support it)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

INSERT statement for SQL

A

Insert into UserInterests(Email, Interest, SinceAge) Values(‘user12@gt.edu’, ‘Reading’, 5);

only will insert a single row at a time

can be rejected if you try to insert a row if the primary key already exists in the table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Delete statement in SQL

A

delete from UserInterests where Interest=”Swimming”;

deletes multiple rows on matching rows

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Update statement in SQL

A

update UserInterests set Interest=’Rock Music’ where Email=”user3@gt.edu’ and Interest=’Music’;

can affect multiple rows if the condition succeeds

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

General SQL Query Syntax

A

Select column1, column2, … columnN from table1, table2, … tableM where condition;

column is the name of a column in some table like BirthYear

table is the name of a table like RegularUser

condition may compare values of columns to constraints or to each other like BirthYear > 1985 or CurrentCity = HomeTown

conditions can be combined with ANT, OR, Not, and ()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Selection and the * wildcard

Find all RegularUser’s

A

SELECT Email, BirthYear, Sex, CurrentCity, HomeTown from RegularUser;

or

Select * from RegularUser;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Selection with a WHERE clause

Find all RegularUser’s with HomeTown Atlanta

A

Select * from RegularUser where HomeTown = ‘Atlanta’;

returns all rows that matches “Atlanta” for hometown

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Selection with composite WHERE clause

Find all RegularUser’s who have the same CurrentCity and HomeTown or who live in Atlanta

A

Select * from RegularUser WHERE CurrentCity = HomeTown OR HomeTown=’Atlanta’;

Returns rows that matches any of the two conditions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Projection in SQL

Find Email, BirthYear, and Sex for RegularUser’s in Atlanta

A

Select Email, BirthYear, Sex from RegularUser where HomeTown=’Atlanta’;

includes only the rows that you select. But does include duplicates by default!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Distinct - tables may have duplicate rows

A

Select Distinct(Sex) from RegularUser where HomeTown=’Atlanta’;

Eliminates duplicates like in relational algebra and calculus for all the columns that you mark as distinct

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Natural Inner Join – and dot notation

Find Email, BirthYear, and Salary for RegularUser’s who have a salary by combining RegularUser data with YearSalary data

A

SELECT Email, RegularUser.BirthYear, Salary from RegularUser, YearSalary WHERE RegularUser.BirthYear = YearSalary.BirthYear

remember this only returns rows where the join condition is true

the dot notation is only necessary when there’s ambiguity

Remember we can also do NATURAL JOIN!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Natural Join in SQL!

A

SELECT Email, RegularUser.BirthYear, Salary from RegularUser NATURAL JOIN YearSalary;

This works just like using =

17
Q

Natural Inner Join - Aliases

A

Select Email, R.BirthYear, Salary From RegularUser as R, YearSalary as Y Where R.BirthYear = Y.BirthYear;

Aliases save typing

Aliases are used to disambiguate table references

Aliases must be used when joining a table to itself!

18
Q

Left Outer Join

Find Email, BirthYear, and Salary for RegularUser’s who have a Salary by combining RegularUser data with YearSalary data. Return Email and BirthYear even when the RegularUser has no Salary.

A

Select Email, RegularUser.BirthYear, Salary From RegularUser LEFT OUTER JOIN YearSalary;

All rows from the first table have NULL for the extra columns

19
Q

String Matching

Find data about RegularUser’s who live in a CurrentCity that starts with “San”

A

SELECT Email, Sex, Current City FROM RegularUser WHERE CurrentCity LIKE ‘San%’;

% matches any string, including the empty string

20
Q

Wildcard %

A

matches any string, including the empty string

21
Q

Wildcard _

A

matches any single character. ‘A____’ A with 4 characters

22
Q

Sorting! (oh no!)

Find data about RegularUser’s who are Males. Sort the data by ascending CurrentCity

A

Select Email, Sex, CurrentCity From RegularUser Where Sex=’M’ Order By CurrentCity Asc;

sorts it in alphabetical order

Can sort on multiple columns (one by asc, another with desc)

23
Q

Set Operations - Union

Find all CurrentCity’s and HomeTowns without duplicates

A

Select CurrentCity from RegularUser UNION Select HomeTown From RegularUser;

NO DUPLICATES!

24
Q

What Union command returns duplicates?

A

Select CurrentCity from RegularUser Union All Select HomeTown from RegularUser;

25
Q

Set Operations - Intersect

Find all cities that are CurrentCity’s for someone and HomeTown’s for someone. Do not include duplicates.

A

SELECT CurrentCity FROM RegularUser INTERSECT Select HomeTown FROM RegularUser;

26
Q

What Intersection command returns duplicates?

A

SELECT CurrentCity FROM RegularUser INTERSECT ALL Select HomeTown FROM RegularUser;

If a value intersects and appears twice in the same column, it will be listed twice with ALL

27
Q

Set Operations - Except

Find all cities that are CurrentCity’s but exclude those that are HomeTown’s. Do not include duplicates.

A

SELECT CurrentCity FROM RegularUser EXCEPT SELECT HomeTown FROM RegularUser;

28
Q

Except with duplicates?

A

SELECT CurrentCity FROM RegularUser EXCEPT ALL SELECT HomeTown FROM RegularUser;

If a value intersects is in the first column and appears twice in the that column, it will be listed twice with ALL

29
Q

List some built in functions

A

count, sum, avg, min, max

30
Q

Count the number of RegularUser’s

A

Select count(*) FROM RegularUser;

31
Q

Find Email and BirthYear for the Youngest Female RegularUser

A

Select Email, max(BirthYear) from RegularUser where Sex=’F’;

32
Q

Group By

Group UserInterests on Email.

A

Select Email, UserInterests Group By Email

33
Q

Group By (2)

For each group, return the email, the number of interests, and the average sinceage for each group

group userinterests on email

sort the result by ascending number of interests

A

SELECT Email, count(*) AS NumInt, avg(SinceAge), AvgAge FROM UserInterests GROUP BY Email ORDER BY NumInt ASC;

34
Q

Having - condition on the group

A

having is like a “where” but for Group By

35
Q

Nested Queries - In/Not In

A

IN means in the result rows of the subquery

36
Q

Find email and interest for RegularUser’s in Atlanta using nested queries

A

Select Email, Interest from UserInterests Where Email IN (Select Email FROM RegularUser WHERE HomeTown = ‘Atlanta’);

As an alternative for this, we can just use a natural join

37
Q

Nested Queries: =, , etc

Find CurentCity’s with at least one RegularUser with a Salary that’s higher than all Salaries of RegularUsers with Hometown Austin

A

SELECT CurrentCity FROM User R, YearSalary Y WHERE R.BirthYear = Y.BirthYear AND Salary > ALL (SELECT Salary FROM RegularUser R, YearSalary Y Where R.BirthYear = Y.BirthYear AND HomeTown = ‘Austin’);

38
Q

Nested Queries - Correlated

Find Email and BirthYear of RegularUsers who have no Interests

A

Select R.Email, BirthYear from RegularUser R Where not exist (Select * From UserInterests U Where U.Email = R.Email)

R does not exist in inner query. It is a “reference out of scope”

think of it as a sub-query evaluated once for each row of the outer query