Exam 3 Flashcards
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!