Information Resource Engineering SQL3. Recap- SQL Rules and Conventions  Certain ‘keywords’ are ‘reserved’ and have special meaning in SQL e.g.: SELECT,

Slides:



Advertisements
Similar presentations
Virtual training week 4 structured query language (SQL)
Advertisements

1Eyad Alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
Introduction to Structured Query Language (SQL)
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Restricting and Sorting Data. Consider the table employee(employee_id,last_name,job_id, department_id ) assume that you want to display all the employees.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Introduction to Structured Query Language (SQL)
Microsoft Access 2010 Chapter 7 Using SQL.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition.
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
Ceng 356-Lab1. Objectives After completing this lesson, you should be able to do the following: Get Familiar with the development environment List the.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Rationale Aspiring Database Developers should be able to efficiently query and maintain databases. This module will help students learn the Structured.
Chapter 2 Basic SQL SELECT Statements
Chapter 2 Basic SQL SELECT Statements Oracle 10g: SQL.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
2 Writing Basic SELECT Statements. 1-2 Copyright  Oracle Corporation, All rights reserved. Capabilities of SQL SELECT Statements Selection Projection.
Copyright  Oracle Corporation, All rights reserved. Writing Basic SQL Statements.
Information Resource Engineering SQL4. Recap - Ordering Output  Usually, the order of rows returned in a query result is undefined.  The ORDER BY clause.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
Structured Query Language
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
Working with Columns, Characters, and Rows. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Apply the concatenation.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
1 Chapter 2 Basic SQL SELECT Statements. 2 Chapter Objectives Distinguish between an RDBMS and an ORDBMS Identify keywords, mandatory clauses, and optional.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
Limiting Selected Rows. 2-2 Objectives Sort row output using the ORDER BY clause. Sort row output using the ORDER BY clause. Enter search criteria using.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Tarik Booker CS 122. What we will cover… Tables (review) SELECT statement DISTINCT, Calculated Columns FROM Single tables (for now…) WHERE Date clauses,
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
Copyright س Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
1 Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Retrieving Data Using the SQL SELECT Statement
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Writing Basic SQL SELECT Statements
Basic select statement
Using the Set Operators
Writing Basic SQL SELECT Statements
Using the Set Operators
Retrieving Data Using the SQL SELECT Statement
Access: SQL Participation Project
Writing Basic SQL SELECT Statements
Writing Basic SQL Statements
Retrieving Data Using the SQL SELECT Statement
Restricting and Sorting Data
Using the Set Operators
Shelly Cashman: Microsoft Access 2016
Restricting and Sorting Data
Presentation transcript:

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 ‘;’ CharacterRange LetterA-Z a-z Number0-9 Other_

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’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’);

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,Cost FROM Course WHERE CourseCode BETWEEN ‘C002’ AND ‘C0004’; Course Code CourseDescriptionCost C002Intro to Word£60.00 C003Intro to Excel£60.00 C004Advanced Web£ Course Code CourseDescriptionCost C002Intro 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 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 *

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

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

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

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 > ; 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 EmpNoNameAnnualSalaryBonus E001Fred18, E002BamBam27, E003Barney20, E004Wilma24, E006Pebbles14, E007Betty16, E009Dino30, Employee

Calculated Fields SELECT Name, AnnualSalary,Bonus FROM Employee WHERE ((AnnualSalary/12) + Bonus) > ; EmpNoNameAnnualSalaryBonus E001Fred18, E002BamBam27, E003Barney20, E004Wilma24, E006Pebbles14, E007Betty16, E009Dino30, Employee NameAnnualSalaryBonus BamBam27, Wilma24, Dino30, 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; NameExpr1001 Fred1, BamBam2, Barney1, Wilma2, Pebbles1, Betty1, Dino2, Resulting Output

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

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 EmpNoNameAnnualSalaryBonus E001Fred18, E002BamBam27, E003Barney20, E004Wilma24, E006Pebbles14, E007Betty16, E009Dino30, Employee Resulting Output SELECT EmpNo AS ‘Employee Number’, (AnnualSalary/12) AS “Monthly Salary” FROM Employee; ‘Employee Number’ “Monthly Salary” E0011, E0022, E0031, E0042, E0061, E0071, E0092,500.00

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, E002BamBam27, E003Barney20, E004Wilma24, E006Pebbles14, E007Betty16, E009Dino30, Employee

Column Aliases EmpNoNameAnnualSalaryBonus E001Fred18, E002BamBam27, E003Barney20, E004Wilma24, E006Pebbles14, E007Betty16, E009Dino30, Employee Resulting Output SELECT EmpNo AS ‘Employee Number’, Name, Bonus AS ‘Monthly Bonus’ FROM Employee WHERE Bonus >=100; ‘Employee Number’ Name ‘Monthly Bonus’ E001Fred E002BamBam E003Barney E004Wilma E006Pebbles E007Betty E009Dino300.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 EmpNoDeptNoJob FredD1Salesperson WilmaD2Manager BarneyD3Programmer BettyD2Salesperson PebblesD3Programmer BamBamD1Analyst DinoD4Manager SELECT DeptNo FROM Employee; Resulting Output Employee DeptNo D1 D2 D3 D2 D3 D1 D4

Using Distinct EmpNoDeptNoJob FredD1Salesperson WilmaD2Manager BarneyD3Programmer BettyD2Salesperson PebblesD3Programmer BamBamD1Analyst DinoD4Manager SELECT DISTINCT DeptNo FROM Employee; Resulting Output Employee 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 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

Ordering Output  We can use the WHERE statement to select rows before we order them. EmpNoNameAnnualSalary E003Barney20, E004Wilma24, E002BamBam27, E009Dino30, Employee Resulting Output SELECT EmpNo, Name, AnnualSalary FROM Employee WHERE AnnualSalary > ORDER BY AnnualSalary; EmpNoNameAnnualSalaryBonus E001Fred18, E002BamBam27, E003Barney20, E004Wilma24, E006Pebbles14, E007Betty16, E009Dino30,

Ordering Output EmpNoNameAnnualSalary E009Dino30, E002BamBam27, E004Wilma24, E003Barney20, Employee Resulting Output SELECT EmpNo, Name, AnnualSalary FROM Employee WHERE AnnualSalary > ORDER BY AnnualSalary DESC; EmpNoNameAnnualSalaryBonus E001Fred18, E002BamBam27, E003Barney20, E004Wilma24, E006Pebbles14, E007Betty16, E009Dino30,

Ordering Output Employee EmpNoNameAnnualSalaryBonus E001Fred18, E002BamBam27, E003Barney20, E004Wilma24, E006Pebbles14, E007Betty16, E009Dino30, 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 NameAnnualSalary Pebbles14, Betty16, Fred18, Employee Resulting Output SELECT Name, AnnualSalary FROM Employee WHERE AnnualSalary < ORDER BY AnnualSalary DESC; EmpNoNameAnnualSalaryBonus E001Fred18, E002BamBam27, E003Barney20, E004Wilma24, E006Pebbles14, E007Betty16, E009Dino30,

Ordering Output  We don’t have to output the column we are ordering on. EmpNoNameAnnualSalaryBonus E001Fred18, E002BamBam27, E003Barney20, E004Wilma24, E006Pebbles14, E007Betty16, E009Dino30, EmployeeResulting Output SELECT Name, AnnualSalary FROM Employee ORDER BY Bonus; NameAnnualSalary Fred18, BamBam27, Pebbles14, Betty16, Barney20, Dino30, Wilma24,000.00

Ordering Output  We can order on more than one column EmpNoNameAnnualSalaryBonus E001Fred18, E002BamBam27, E003Barney20, E004Wilma24, E006Pebbles14, E007Betty16, E009Dino30, EmployeeResulting Output SELECT * FROM Employee ORDER BY Bonus,AnnualSalary; EmpNoNameAnnualSalaryBonus E001Fred18, E006Pebbles14, E007Betty16, E002BamBam27, E003Barney20, E009Dino30, E004Wilma24,

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, E002BamBamD1Analyst2, E003BarneyD3Programmer1, E004WilmaD2Manager2, E006PebblesD3Programmer1, E007BettyD2Salesperson1, E009DinoD4Manager2,500.00

Ordering Output Employee EmpNoNameDeptNoJobMonthlySalary E001FredD1Salesperson1, E002BamBamD1Analyst2, E003BarneyD3Programmer1, E004WilmaD2Manager2, E006PebblesD3Programmer1, E007BettyD2Salesperson1, E009DinoD4Manager2, 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; NameDeptNoJobMonthlySalary BamBamD1Analyst2, DinoD4Manager2, WilmaD2Manager2, BarneyD3Programmer1, PebblesD3Programmer1, FredD1Salesperson1, BettyD2Salesperson1,400.00

In Conclusion  We have covered:  BETWEEN, IN and LIKE  Deleting Information.  Column Aliases  Dealing with duplicate rows (DISTINCT)  Ordering output (ORDER BY)