Exam 3 Flashcards
(38 cards)
SQL original name
SEQUEL: Structured English Query Language: Part of System R in 1973
SQL stands for
Structured Query Language
SQL is based on
Based on relational tuple calculus and some algebra
ANSI & ISO Standards for SQL
SQL/86, SQL 89, 92, SQL2, SQL3, Revisions in 2003, 2006, 2008, 2011…
SQL is supported by
IBM DB2, ORACLE, SYSBASE, SqlServer, MySQL, many more (sort of all support it)
INSERT statement for SQL
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
Delete statement in SQL
delete from UserInterests where Interest=”Swimming”;
deletes multiple rows on matching rows
Update statement in SQL
update UserInterests set Interest=’Rock Music’ where Email=”user3@gt.edu’ and Interest=’Music’;
can affect multiple rows if the condition succeeds
General SQL Query Syntax
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 ()
Selection and the * wildcard
Find all RegularUser’s
SELECT Email, BirthYear, Sex, CurrentCity, HomeTown from RegularUser;
or
Select * from RegularUser;
Selection with a WHERE clause
Find all RegularUser’s with HomeTown Atlanta
Select * from RegularUser where HomeTown = ‘Atlanta’;
returns all rows that matches “Atlanta” for hometown
Selection with composite WHERE clause
Find all RegularUser’s who have the same CurrentCity and HomeTown or who live in Atlanta
Select * from RegularUser WHERE CurrentCity = HomeTown OR HomeTown=’Atlanta’;
Returns rows that matches any of the two conditions
Projection in SQL
Find Email, BirthYear, and Sex for RegularUser’s in Atlanta
Select Email, BirthYear, Sex from RegularUser where HomeTown=’Atlanta’;
includes only the rows that you select. But does include duplicates by default!
Distinct - tables may have duplicate rows
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
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
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!
Natural Join in SQL!
SELECT Email, RegularUser.BirthYear, Salary from RegularUser NATURAL JOIN YearSalary;
This works just like using =
Natural Inner Join - Aliases
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!
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.
Select Email, RegularUser.BirthYear, Salary From RegularUser LEFT OUTER JOIN YearSalary;
All rows from the first table have NULL for the extra columns
String Matching
Find data about RegularUser’s who live in a CurrentCity that starts with “San”
SELECT Email, Sex, Current City FROM RegularUser WHERE CurrentCity LIKE ‘San%’;
% matches any string, including the empty string
Wildcard %
matches any string, including the empty string
Wildcard _
matches any single character. ‘A____’ A with 4 characters
Sorting! (oh no!)
Find data about RegularUser’s who are Males. Sort the data by ascending CurrentCity
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)
Set Operations - Union
Find all CurrentCity’s and HomeTowns without duplicates
Select CurrentCity from RegularUser UNION Select HomeTown From RegularUser;
NO DUPLICATES!
What Union command returns duplicates?
Select CurrentCity from RegularUser Union All Select HomeTown from RegularUser;