Download presentation
Presentation is loading. Please wait.
PublishMelinda Simon Modified over 9 years ago
1
Information Resource Engineering SQL3
2
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 ‘;’ CharacterRange LetterA-Z a-z Number0-9 Other_
3
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;
4
Recap-WHERE WHERE statements implement the Select (sometimes called the Restrict) operation in relational algebra. =equals <less than > greater than <>not equals !=not equals
5
Recap- AND’s, OR’s and NOT’s Course Code CourseDescriptionCostTypeCredit Value Day Taught Campus C002Intro to Word60Word Proc.5WedRhondda C003Intro to Excel60Sp.Sheet5ThurPonty C004Advanced Web200Web15WedPonty C005Advanced Access250Database15ThurPonty C006Intermediate Word150Word Proc.10WedPonty Course SELECT CourseCode,CreditValue,DayTaught,Campus FROM Course WHERE CreditValue=5 AND NOT (DayTaught=‘Thur’ OR Campus = ‘Rhondda’);
6
BETWEEN SELECT * FROM Course WHERE Cost >= 150 AND Cost <= 250; SELECT * FROM Course WHERE Cost BETWEEN 150 AND 250; Can also be written as:
7
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’;
8
BETWEEN SELECT CourseCode,CourseDescription,Cost FROM Course WHERE CourseCode BETWEEN ‘C002’ AND ‘C004’; SELECT CourseCode,CourseDescription,Cost FROM Course WHERE CourseCode BETWEEN ‘C002’ AND ‘C0004’; Course Code CourseDescriptionCost C002Intro to Word£60.00 C003Intro to Excel£60.00 C004Advanced Web£200.00 Course Code CourseDescriptionCost C002Intro to Word£60.00
9
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:
10
LIKE Course Code CourseDescriptionTutor C001Networks AdvancedA. Cable C002Intro to WordF.Smith C003Intro to ExcelB.Bloggs C004Advanced WebM. Lamb C005Advanced AccessL. Lamb C006Intermediate WordL.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 *
11
LIKE Course Code CourseDescriptionTutor C001Networks AdvancedA. Cable C002Intro to WordF.Smith C003Intro to ExcelB.Bloggs C004Advanced WebM. Lamb C005Advanced AccessL. Lamb C006Intermediate WordL.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 C001Networks Advanced
12
LIKE Course Code CourseDescriptionTutor C007Networks AdvancedA. Cable C002Intro to WordF.Smith C003Intro to ExcelB.Bloggs C004Advanced WebM. Lamb C005Advanced AccessL. Lamb C006Intermediate WordL.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 C004Advanced Web C005Advanced Access
13
LIKE Course Code CourseDescriptionTutor C001Networks AdvancedA. Cable C002Intro to WordF.Smith C003Intro to ExcelB.Bloggs C004Advanced WebM. Lamb C005Advanced AccessL. Lamb C006Intermediate WordL.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 C001Networks Advanced C004Advanced Web C005Advanced Access
14
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)
15
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:
16
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.
17
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
18
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 EmpNoNameAnnualSalaryBonus E001Fred18,000.00100.00 E002BamBam27,600.00150.00 E003Barney20,400.00200.00 E004Wilma24,000.00400.00 E006Pebbles14,400.00150.00 E007Betty16,800.00150.00 E009Dino30,000.00300.00 Employee
19
Calculated Fields SELECT Name, AnnualSalary,Bonus FROM Employee WHERE ((AnnualSalary/12) + Bonus) > 2000.00; EmpNoNameAnnualSalaryBonus E001Fred18,000.00100.00 E002BamBam27,600.00150.00 E003Barney20,400.00200.00 E004Wilma24,000.00400.00 E006Pebbles14,400.00150.00 E007Betty16,800.00150.00 E009Dino30,000.00300.00 Employee NameAnnualSalaryBonus BamBam27,600.00150.00 Wilma24,000.00400.00 Dino30,000.00300.00 Resulting Output
20
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; NameExpr1001 Fred1,500.00 BamBam2,300.00 Barney1,700.00 Wilma2,000.00 Pebbles1,200.00 Betty1,400.00 Dino2,500.00 Resulting Output
21
Column Aliases Even for those ‘columns’ specified as attributes in the table this can be rather cryptic. EmpNoName E001Fred E002BamBam E003Barney E004Wilma E006Pebbles E007Betty E009Dino SELECT EmpNo, Name FROM Employee; Resulting Output
22
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
23
Column Aliases EmpNoNameAnnualSalaryBonus E001Fred18,000.00100.00 E002BamBam27,600.00150.00 E003Barney20,400.00200.00 E004Wilma24,000.00400.00 E006Pebbles14,400.00150.00 E007Betty16,800.00150.00 E009Dino30,000.00300.00 Employee Resulting Output SELECT EmpNo AS ‘Employee Number’, (AnnualSalary/12) AS “Monthly Salary” FROM Employee; ‘Employee Number’ “Monthly Salary” E0011,500.00 E0022,300.00 E0031,700.00 E0042,000.00 E0061,200.00 E0071,400.00 E0092,500.00
24
Column Aliases Using 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. EmpNoNameAnnualSalaryBonus E001Fred18,000.00100.00 E002BamBam27,600.00150.00 E003Barney20,400.00200.00 E004Wilma24,000.00400.00 E006Pebbles14,400.00150.00 E007Betty16,800.00150.00 E009Dino30,000.00300.00 Employee
25
Column Aliases EmpNoNameAnnualSalaryBonus E001Fred18,000.00100.00 E002BamBam27,600.00150.00 E003Barney20,400.00200.00 E004Wilma24,000.00400.00 E006Pebbles14,400.00150.00 E007Betty16,800.00150.00 E009Dino30,000.00300.00 Employee Resulting Output SELECT EmpNo AS ‘Employee Number’, Name, Bonus AS ‘Monthly Bonus’ FROM Employee WHERE Bonus >=100; ‘Employee Number’ Name ‘Monthly Bonus’ E001Fred100.00 E002BamBam150.00 E003Barney200.00 E004Wilma400.00 E006Pebbles150.00 E007Betty150.00 E009Dino300.00
26
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.
27
Duplicate Rows EmpNoDeptNoJob FredD1Salesperson WilmaD2Manager BarneyD3Programmer BettyD2Salesperson PebblesD3Programmer BamBamD1Analyst DinoD4Manager SELECT DeptNo FROM Employee; Resulting Output Employee DeptNo D1 D2 D3 D2 D3 D1 D4
28
Using Distinct EmpNoDeptNoJob FredD1Salesperson WilmaD2Manager BarneyD3Programmer BettyD2Salesperson PebblesD3Programmer BamBamD1Analyst DinoD4Manager SELECT DISTINCT DeptNo FROM Employee; Resulting Output Employee DeptNo D1 D2 D3 D4
29
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.
30
Ordering Output EmpNoDeptNoJob FredD1Salesperson WilmaD2Manager BarneyD3Programmer BettyD2Salesperson PebblesD3Programmer BamBamD1Analyst DinoD4Manager SELECT * FROM Employee ORDER BY DeptNo; Resulting Output Employee EmpNoDeptNoJob FredD1Salesperson BamBamD1Analyst WilmaD2Manager BettyD2Salesperson BarneyD3Programmer PebblesD3Programmer DinoD4Manager
31
Ordering Output We can use the WHERE statement to select rows before we order them. EmpNoNameAnnualSalary E003Barney20,400.00 E004Wilma24,000.00 E002BamBam27,600.00 E009Dino30,000.00 Employee Resulting Output SELECT EmpNo, Name, AnnualSalary FROM Employee WHERE AnnualSalary > 20000 ORDER BY AnnualSalary; EmpNoNameAnnualSalaryBonus E001Fred18,000.00100.00 E002BamBam27,600.00150.00 E003Barney20,400.00200.00 E004Wilma24,000.00400.00 E006Pebbles14,400.00150.00 E007Betty16,800.00150.00 E009Dino30,000.00300.00
32
Ordering Output EmpNoNameAnnualSalary E009Dino30,000.00 E002BamBam27,600.00 E004Wilma24,000.00 E003Barney20,400.00 Employee Resulting Output SELECT EmpNo, Name, AnnualSalary FROM Employee WHERE AnnualSalary > 20000 ORDER BY AnnualSalary DESC; EmpNoNameAnnualSalaryBonus E001Fred18,000.00100.00 E002BamBam27,600.00150.00 E003Barney20,400.00200.00 E004Wilma24,000.00400.00 E006Pebbles14,400.00150.00 E007Betty16,800.00150.00 E009Dino30,000.00300.00
33
Ordering Output Employee EmpNoNameAnnualSalaryBonus E001Fred18,000.00100.00 E002BamBam27,600.00150.00 E003Barney20,400.00200.00 E004Wilma24,000.00400.00 E006Pebbles14,400.00150.00 E007Betty16,800.00150.00 E009Dino30,000.00300.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
34
Ordering Output NameAnnualSalary Pebbles14,400.00 Betty16,800.00 Fred18,000.00 Employee Resulting Output SELECT Name, AnnualSalary FROM Employee WHERE AnnualSalary < 20000 ORDER BY AnnualSalary DESC; EmpNoNameAnnualSalaryBonus E001Fred18,000.00100.00 E002BamBam27,600.00150.00 E003Barney20,400.00200.00 E004Wilma24,000.00400.00 E006Pebbles14,400.00150.00 E007Betty16,800.00150.00 E009Dino30,000.00300.00
35
Ordering Output We don’t have to output the column we are ordering on. EmpNoNameAnnualSalaryBonus E001Fred18,000.00100.00 E002BamBam27,600.00150.00 E003Barney20,400.00200.00 E004Wilma24,000.00400.00 E006Pebbles14,400.00150.00 E007Betty16,800.00150.00 E009Dino30,000.00300.00 EmployeeResulting Output SELECT Name, AnnualSalary FROM Employee ORDER BY Bonus; NameAnnualSalary Fred18,000.00 BamBam27,600.00 Pebbles14,400.00 Betty16,800.00 Barney20,400.00 Dino30,000.00 Wilma24,000.00
36
Ordering Output We can order on more than one column EmpNoNameAnnualSalaryBonus E001Fred18,000.00100.00 E002BamBam27,600.00150.00 E003Barney20,400.00200.00 E004Wilma24,000.00400.00 E006Pebbles14,400.00150.00 E007Betty16,800.00150.00 E009Dino30,000.00300.00 EmployeeResulting Output SELECT * FROM Employee ORDER BY Bonus,AnnualSalary; EmpNoNameAnnualSalaryBonus E001Fred18,000.00100.00 E006Pebbles14,400.00150.00 E007Betty16,800.00150.00 E002BamBam27,600.00150.00 E003Barney20,400.00200.00 E009Dino30,000.00300.00 E004Wilma24,000.00400.00
37
Ordering Output Using 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 EmpNoNameDeptNoJobMonthlySalary E001FredD1Salesperson1,500.00 E002BamBamD1Analyst2,300.00 E003BarneyD3Programmer1,700.00 E004WilmaD2Manager2,000.00 E006PebblesD3Programmer1,200.00 E007BettyD2Salesperson1,400.00 E009DinoD4Manager2,500.00
38
Ordering Output Employee EmpNoNameDeptNoJobMonthlySalary E001FredD1Salesperson1,500.00 E002BamBamD1Analyst2,300.00 E003BarneyD3Programmer1,700.00 E004WilmaD2Manager2,000.00 E006PebblesD3Programmer1,200.00 E007BettyD2Salesperson1,400.00 E009DinoD4Manager2,500.00 SELECT Name, DeptNo, Job,MonthlySalary FROM Employee ORDER BY Job, MonthlySalary DESC;
39
Ordering Output Resulting Output SELECT Name, DeptNo, Job,MonthlySalary FROM Employee ORDER BY Job, MonthlySalary DESC; NameDeptNoJobMonthlySalary BamBamD1Analyst2,300.00 DinoD4Manager2,500.00 WilmaD2Manager2,000.00 BarneyD3Programmer1,700.00 PebblesD3Programmer1,200.00 FredD1Salesperson1,500.00 BettyD2Salesperson1,400.00
40
In Conclusion We have covered: BETWEEN, IN and LIKE Deleting Information. Column Aliases Dealing with duplicate rows (DISTINCT) Ordering output (ORDER BY)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.