Zack Lane ReCAP Coordinator February 2012 ReCAP Columbia University.
Information Resource Engineering SQL3. Recap- SQL Rules and Conventions Certain ‘keywords’ are...
-
Upload
melinda-simon -
Category
Documents
-
view
216 -
download
0
Transcript of Information Resource Engineering SQL3. Recap- SQL Rules and Conventions Certain ‘keywords’ are...
Information Resource Engineering
SQL3
Recap-SQL Rules and Conventions Certain ‘keywords’ are ‘reserved’ and have
special meaning in SQL e.g.: SELECT, FROM
All SQL keywords are shown in UPPERCASE Names can be made from:
SQL statements all end with a ‘;’
Character RangeLetter A-Z a-zNumber 0-9Other _
Recap-SELECT SELECT statements implement the
PROJECT operation in relational algebra.
Column names can be in any order
SELECT column_name, column_name...FROM table_name;
Recap-WHERE WHERE statements implement the
Select (sometimes called the Restrict) operation in relational algebra.
= equals
< less than
> greater than
<> not equals
!= not equals
Recap- AND’s, OR’s and NOT’sCourse Code
CourseDescription Cost Type Credit Value
Day Taught
Campus
C002 Intro to Word 60 Word Proc. 5 Wed Rhondda
C003 Intro to Excel 60 Sp.Sheet 5 Thur Ponty
C004 Advanced Web 200 Web 15 Wed Ponty
C005 Advanced Access 250 Database 15 Thur Ponty
C006 Intermediate Word
150 Word Proc. 10 Wed Ponty
Course
SELECT CourseCode,CreditValue,DayTaught,Campus
FROM Course
WHERE CreditValue=5 AND NOT (DayTaught=‘Thur’ OR Campus = ‘Rhondda’);
BETWEEN
SELECT *
FROM Course
WHERE Cost >= 150 AND Cost <= 250;
SELECT *
FROM Course
WHERE Cost BETWEEN 150 AND 250;
Can also be written as:
BETWEEN
SELECT *
FROM Course
WHERE CourseCode BETWEEN ‘C001’ AND ‘C004’;
We can also use BETWEEN in comparisons between other data types:
But be careful, as the ‘ordering’ recognised by SQL may differ from your interpretation.
SELECT *
FROM Course
WHERE CourseCode BETWEEN ‘C001’ AND ‘C0004’;
BETWEEN
SELECT CourseCode,CourseDescription,Cost
FROM Course
WHERE CourseCode BETWEEN ‘C002’ AND ‘C004’;
SELECT CourseCode,CourseDescription,CostFROM Course
WHERE CourseCode BETWEEN ‘C002’ AND ‘C0004’;
CourseCode
CourseDescription
Cost
C002 Intro to Word £60.00
C003 Intro to Excel £60.00
C004 Advanced Web £200.00
CourseCode
CourseDescription
Cost
C002 Intro to Word £60.00
IN
SELECT *
FROM Course
WHERE Type = ‘Word Proc.’ OR Type = ‘Web’ OR Type = ‘Database’;
SELECT *
FROM Course
WHERE Type IN (‘Word Proc.’,‘Web’,‘Database’);
Can also be written as:
LIKE
Course Code
CourseDescription Tutor
C001 Networks Advanced
A. Cable
C002 Intro to Word F.Smith
C003 Intro to Excel B.Bloggs
C004 Advanced Web M. Lamb
C005 Advanced Access L. Lamb
C006 Intermediate Word L.Ranger
Sometimes we want to query on ‘part’ of an attribute (field)
What if we wanted a list of all the ‘advanced’ courses
We can use ‘LIKE’ combined with a ‘wildcard’
The ‘wildcard’ used in Access SQL is *
LIKE
Course Code
CourseDescription Tutor
C001 Networks Advanced
A. Cable
C002 Intro to Word F.Smith
C003 Intro to Excel B.Bloggs
C004 Advanced Web M. Lamb
C005 Advanced Access L. Lamb
C006 Intermediate Word
L.Ranger
However, we have to think carefully about where we place the wildcard.
SELECT CourseCode,CourseDescription
FROM Course
WHERE CourseDescription LIKE ‘*Advanced’;
Resulting Output
1 Row
Course Code
CourseDescription
C001 Networks Advanced
LIKE
Course Code
CourseDescription Tutor
C007 Networks Advanced
A. Cable
C002 Intro to Word F.Smith
C003 Intro to Excel B.Bloggs
C004 Advanced Web M. Lamb
C005 Advanced Access L. Lamb
C006 Intermediate Word
L.Ranger
However, we have to think carefully about where we place the wildcard.
SELECT CourseCode,CourseDescription
FROM Course
WHERE CourseDescription LIKE ‘Advanced*’;
Resulting Output
2 Rows
Course Code
CourseDescription
C004 Advanced Web
C005 Advanced Access
LIKE
Course Code
CourseDescription Tutor
C001 Networks Advanced
A. Cable
C002 Intro to Word F.Smith
C003 Intro to Excel B.Bloggs
C004 Advanced Web M. Lamb
C005 Advanced Access L. Lamb
C006 Intermediate Word
L.Ranger
However, we have to think carefully about where we place the wildcard.
SELECT CourseCode,CourseDescription
FROM Course
WHERE CourseDescription LIKE ‘*Advanced*’;
Resulting Output
3 Rows
Course Code
CourseDescription
C001 Networks Advanced
C004 Advanced Web
C005 Advanced Access
Calculated Fields To use a calculated (derived) field, you
specify an SQL expression in the SELECT list. An SQL expression can involve addition,
subtraction, multiplication and division.
SELECT Name, AnnualSalary/12
FROM Employee;
For example:
Will output the monthly salary of employees
(Where AnnualSalary = the annual (yearly) salary)
Calculated Fields Parentheses can be used to build
complex expressions.
SELECT Name, (AnnualSalary – (AnnualSalary*0.3))/12
FROM Employee;
For example, to output the net monthly salary of employees (after tax at 30% has been subtracted) we can use:
Calculated Fields More than one table column can be
used in a calculated column.
SELECT Name,(((AnnualSalary – (AnnualSalary*0.3))/12)+MonthlyExpenses)
FROM Employee;
For example, to output the net monthly salary of employees (after tax at 30% has been subtracted and monthly expenses added) we can use:
Where both AnnualSalary and Expenses are attributes within the Table Employee.
Calculated Fields We can use calculated fields in the
WHERE part of our SQL statement:
SELECT Name, AnnualSalary/12,
FROM Employee
WHERE AnnualSalary/12 > 2000.00;
For example, to output those employees that have a monthly salary of more than £2,000, we can use
Calculated Fields Using the following table, produce the SQL statement that would output all those employees that earn more than £2000 each month (Bonus = their Monthly Bonus)
Monthly Salary = (AnnualSalary/12) + Bonus
EmpNo
NameAnnualSalary
Bonus
E001 Fred 18,000.00 100.00
E002 BamBam
27,600.00 150.00
E003 Barney 20,400.00 200.00
E004 Wilma 24,000.00 400.00
E006 Pebbles 14,400.00 150.00
E007 Betty 16,800.00 150.00
E009 Dino 30,000.00 300.00
Employee
Calculated Fields
SELECT Name, AnnualSalary,Bonus
FROM Employee
WHERE ((AnnualSalary/12) + Bonus) > 2000.00;
EmpNo
NameAnnualSalary
Bonus
E001 Fred 18,000.00 100.00
E002 BamBam
27,600.00 150.00
E003 Barney 20,400.00 200.00
E004 Wilma 24,000.00 400.00
E006 Pebbles 14,400.00 150.00
E007 Betty 16,800.00 150.00
E009 Dino 30,000.00 300.00
Employee
NameAnnualSalary
Bonus
BamBam
27,600.00 150.00
Wilma 24,000.00 400.00
Dino 30,000.00 300.00
Resulting Output
Column Aliases When displaying the result of a query, the
selected column’s name is used as the heading. However, when using a Calculated Field the
‘column’ heading is not particularly descriptive:
SELECT Name, AnnualSalary/12
FROM Employee;Name Expr1001
Fred 1,500.00
BamBam 2,300.00
Barney 1,700.00
Wilma 2,000.00
Pebbles 1,200.00
Betty 1,400.00
Dino 2,500.00
Resulting Output
Column Aliases Even for those ‘columns’ specified as
attributes in the table this can be rather cryptic.
EmpNo Name
E001 Fred
E002 BamBam
E003 Barney
E004 Wilma
E006 Pebbles
E007 Betty
E009 Dino
SELECT EmpNo, Name
FROM Employee;
Resulting Output
Column Aliases You can change a columns heading by
using an Alias. A column alias gives the column an
alternative heading on output.
Any Alias with a ‘blank’ or ‘space’ must be surrounded by quotes. Unfortunately, these also appear in the heading output.
SELECT columnname, columnname AS aliasname, …
FROM tablename
Column Aliases
EmpNo
NameAnnualSalary
Bonus
E001 Fred 18,000.00 100.00
E002 BamBam
27,600.00 150.00
E003 Barney 20,400.00 200.00
E004 Wilma 24,000.00 400.00
E006 Pebbles 14,400.00 150.00
E007 Betty 16,800.00 150.00
E009 Dino 30,000.00 300.00
Employee Resulting Output
SELECT EmpNo AS ‘Employee Number’, (AnnualSalary/12) AS “Monthly Salary”
FROM Employee;
‘Employee Number’
“Monthly Salary”
E001 1,500.00
E002 2,300.00
E003 1,700.00
E004 2,000.00
E006 1,200.00
E007 1,400.00
E009 2,500.00
Column AliasesUsing the following table, output a report with headings
Employee Number Name Monthly Bonus
For those employees that have a bonus equal to or greater than £100.
EmpNo NameAnnualSalary
Bonus
E001 Fred 18,000.00 100.00
E002 BamBam 27,600.00 150.00
E003 Barney 20,400.00 200.00
E004 Wilma 24,000.00 400.00
E006 Pebbles 14,400.00 150.00
E007 Betty 16,800.00 150.00
E009 Dino 30,000.00 300.00
Employee
Column Aliases
EmpNo
NameAnnualSalary
Bonus
E001 Fred 18,000.00 100.00
E002 BamBam
27,600.00 150.00
E003 Barney 20,400.00 200.00
E004 Wilma 24,000.00 400.00
E006 Pebbles 14,400.00 150.00
E007 Betty 16,800.00 150.00
E009 Dino 30,000.00 300.00
Employee Resulting Output
SELECT EmpNo AS ‘Employee Number’, Name, Bonus AS ‘Monthly Bonus’
FROM Employee
WHERE Bonus >=100;
‘Employee Number’
Name‘Monthly Bonus’
E001 Fred 100.00
E002 BamBam 150.00
E003 Barney 200.00
E004 Wilma 400.00
E006 Pebbles 150.00
E007 Betty 150.00
E009 Dino 300.00
Duplicate Rows Unless specified, the results of a query
will be returned with duplicate rows.
To eliminate duplicate values, we must include the DISTINCT clause in the SELECT command.
Duplicate Rows
EmpNoDeptNo
Job
Fred D1 Salesperson
Wilma D2 Manager
Barney D3 Programmer
Betty D2 Salesperson
Pebbles D3 Programmer
BamBam D1 Analyst
Dino D4 Manager
SELECT DeptNo
FROM Employee;
Resulting OutputEmployee
DeptNo
D1
D2
D3
D2
D3
D1
D4
Using Distinct
EmpNoDeptNo
Job
Fred D1 Salesperson
Wilma D2 Manager
Barney D3 Programmer
Betty D2 Salesperson
Pebbles D3 Programmer
BamBam D1 Analyst
Dino D4 Manager
SELECT DISTINCT DeptNo
FROM Employee;
Resulting OutputEmployee
DeptNo
D1
D2
D3
D4
Ordering Output Usually, the order of rows returned in a
query result is undefined.
The ORDER BY clause sets the sequence for outputting selected information.
This can either be: Ascending order ASC (default)
Descending order DESC.
If used the ORDER BY must always be the last clause in the SELECT command.
Ordering Output
EmpNoDeptNo
Job
Fred D1 Salesperson
Wilma D2 Manager
Barney D3 Programmer
Betty D2 Salesperson
Pebbles D3 Programmer
BamBam D1 Analyst
Dino D4 Manager
SELECT *
FROM Employee
ORDER BY DeptNo;
Resulting OutputEmployee
EmpNoDeptNo
Job
Fred D1 Salesperson
BamBam D1 Analyst
Wilma D2 Manager
Betty D2 Salesperson
Barney D3 Programmer
Pebbles D3 Programmer
Dino D4 Manager
Ordering Output We can use the WHERE statement to select rows
before we order them.
EmpNo
Name AnnualSalary
E003 Barney 20,400.00
E004 Wilma 24,000.00
E002 BamBam 27,600.00
E009 Dino 30,000.00
EmployeeResulting Output
SELECT EmpNo, Name, AnnualSalary
FROM Employee
WHERE AnnualSalary > 20000
ORDER BY AnnualSalary;
EmpNo
NameAnnualSalary
Bonus
E001 Fred 18,000.00 100.00
E002 BamBam
27,600.00 150.00
E003 Barney 20,400.00 200.00
E004 Wilma 24,000.00 400.00
E006 Pebbles 14,400.00 150.00
E007 Betty 16,800.00 150.00
E009 Dino 30,000.00 300.00
Ordering Output
EmpNo
Name AnnualSalary
E009 Dino 30,000.00
E002 BamBam 27,600.00
E004 Wilma 24,000.00
E003 Barney 20,400.00
EmployeeResulting Output
SELECT EmpNo, Name, AnnualSalary
FROM Employee
WHERE AnnualSalary > 20000
ORDER BY AnnualSalary DESC;
EmpNo
NameAnnualSalary
Bonus
E001 Fred 18,000.00 100.00
E002 BamBam
27,600.00 150.00
E003 Barney 20,400.00 200.00
E004 Wilma 24,000.00 400.00
E006 Pebbles 14,400.00 150.00
E007 Betty 16,800.00 150.00
E009 Dino 30,000.00 300.00
Ordering Output
EmployeeEmpNo
NameAnnualSalary
Bonus
E001 Fred 18,000.00 100.00
E002 BamBam
27,600.00 150.00
E003 Barney 20,400.00 200.00
E004 Wilma 24,000.00 400.00
E006 Pebbles 14,400.00 150.00
E007 Betty 16,800.00 150.00
E009 Dino 30,000.00 300.00
Using the following table, output those employees that have an Annual Salary of less than £20,000. Output Name and Annual Salary in Descending Annual Salary order
Ordering Output
Name AnnualSalary
Pebbles 14,400.00
Betty 16,800.00
Fred 18,000.00
EmployeeResulting Output
SELECT Name, AnnualSalary
FROM Employee
WHERE AnnualSalary < 20000
ORDER BY AnnualSalary DESC;
EmpNo
NameAnnualSalary
Bonus
E001 Fred 18,000.00 100.00
E002 BamBam
27,600.00 150.00
E003 Barney 20,400.00 200.00
E004 Wilma 24,000.00 400.00
E006 Pebbles 14,400.00 150.00
E007 Betty 16,800.00 150.00
E009 Dino 30,000.00 300.00
Ordering Output We don’t have to output the column we are
ordering on.
EmpNo
NameAnnualSalary
Bonus
E001 Fred 18,000.00 100.00
E002 BamBam
27,600.00 150.00
E003 Barney 20,400.00 200.00
E004 Wilma 24,000.00 400.00
E006 Pebbles 14,400.00 150.00
E007 Betty 16,800.00 150.00
E009 Dino 30,000.00 300.00
Employee Resulting Output
SELECT Name, AnnualSalary
FROM Employee
ORDER BY Bonus;
NameAnnualSalary
Fred 18,000.00
BamBam
27,600.00
Pebbles 14,400.00
Betty 16,800.00
Barney 20,400.00
Dino 30,000.00
Wilma 24,000.00
Ordering Output We can order on more than one column
EmpNo
NameAnnualSalary
Bonus
E001 Fred 18,000.00 100.00
E002 BamBam
27,600.00 150.00
E003 Barney 20,400.00 200.00
E004 Wilma 24,000.00 400.00
E006 Pebbles 14,400.00 150.00
E007 Betty 16,800.00 150.00
E009 Dino 30,000.00 300.00
Employee Resulting Output
SELECT *
FROM Employee
ORDER BY Bonus,AnnualSalary;
EmpNo
NameAnnualSalary
Bonus
E001 Fred 18,000.00 100.00
E006 Pebbles 14,400.00 150.00
E007 Betty 16,800.00 150.00
E002 BamBam
27,600.00 150.00
E003 Barney 20,400.00 200.00
E009 Dino 30,000.00 300.00
E004 Wilma 24,000.00 400.00
Ordering OutputUsing the following table, produce the SQL statement that would output the employee name, department number, job and monthly salary in job then descending salary order.
Employee
EmpNo
NameDeptNo
JobMonthlySalary
E001 Fred D1 Salesperson
1,500.00
E002 BamBam
D1 Analyst 2,300.00
E003 Barney D3 Programmer
1,700.00
E004 Wilma D2 Manager 2,000.00
E006 Pebbles
D3 Programmer
1,200.00
E007 Betty D2 Salesperson
1,400.00
E009 Dino D4 Manager 2,500.00
Ordering OutputEmployee
EmpNo
NameDeptNo
JobMonthlySalary
E001 Fred D1 Salesperson
1,500.00
E002 BamBam
D1 Analyst 2,300.00
E003 Barney D3 Programmer
1,700.00
E004 Wilma D2 Manager 2,000.00
E006 Pebbles
D3 Programmer
1,200.00
E007 Betty D2 Salesperson
1,400.00
E009 Dino D4 Manager 2,500.00
SELECT Name, DeptNo, Job,MonthlySalary
FROM Employee
ORDER BY Job, MonthlySalary DESC;
Ordering Output
Resulting Output
SELECT Name, DeptNo, Job,MonthlySalary
FROM Employee
ORDER BY Job, MonthlySalary DESC;
NameDeptNo
JobMonthlySalary
BamBam
D1 Analyst 2,300.00
Dino D4 Manager 2,500.00
Wilma D2 Manager 2,000.00
Barney D3 Programmer
1,700.00
Pebbles D3 Programmer
1,200.00
Fred D1 Salesperson
1,500.00
Betty D2 Salesperson
1,400.00
In Conclusion We have covered:
BETWEEN, IN and LIKE
Deleting Information.
Column Aliases
Dealing with duplicate rows (DISTINCT)
Ordering output (ORDER BY)