Presentation is loading. Please wait.

Presentation is loading. Please wait.

Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete.

Similar presentations


Presentation on theme: "Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete."— Presentation transcript:

1 Session 4 SQL Structured Query Language

2 SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

3 Access SQL Where to practice your SQL.

4 Main SQL Statements SELECT INSERT UPDATE DELETE

5 Rules & Conventions SQL is free-format, like HTML, but typing clauses on separate lines makes it easier to read. Type reserved words in capitals. All non-numeric data must be enclosed in single quotes e.g. 'myname'. The order of the clauses in a SELECT statement cannot be changed. Close all queries with a semi-colon ;

6 List all Employees SELECT EmpNo, Bno, Name, Address, DOB, Salary FROM Employee; Employee {Empno, Bno, Name, Address, DOB, Position, Salary} Corresponds to field names in the database. Tip: don't leave spaces in field names when creating the DB. * = Wildcard character. In SQL this is used to return all records. Corresponds to table names in the database. SELECT * FROM Employee;

7 Retrieve an Ordered List SELECT Name, Address FROM Employee ORDER BY Name; Employee {Empno, Bno, Name, Address, DOB, Position, Salary}

8 How Many Employees Share the Same Postcode? SELECT Postcode, COUNT(*) AS Total FROM Employee GROUP BY Postcode; Employee {Empno, Bno, Name, Address, DOB, Position, Salary} PostcodeTotal B15 1BQ6 B3 1LX3 B3 2AS2 B4 6DD1

9 Employee's Monthly Salary? SELECT Name, Salary/12 FROM Employee; Employee {Empno, Bno, Name, Address, DOB, Position, Salary} NameExpr1001 Smith2666.66 Foster2333.33 Jones2208.33 Patel2083.33 Chen2583.33 Mathematical Operators + Add - Subtract * Multiply / Divide Note the default column name.

10 SQL – Aggregate Functions MIN Returns lowest value in a set of values MAX Returns highest value in a set of values SUM Provides the sum of a set of values. COUNT AVG Returns average of a set of values. Nulls are ignored.

11 Total Salary Bill for the Year SELECT SUM(Salary) AS TotalSalaries FROM Employee; TotalSalaries £142,500.00 Employee {Empno, Bno, Name, Address, DOB, Position, Salary}

12 Comparison Operators =equals <is less than >is greater than <= is less than or equal to >=is greater than or equal to <>is not equal to !=is not equal to

13 Where Five basic search conditions Comparison Compare the value of one expression to the value of another expression Range Test whether the value of an expression falls within a specified range of values Set Membership Test whether the value of an expression equals one of a set of values Pattern Match Test whether a string matches a specified pattern Null Test whether a column has a null value

14 Comparison - Salary Above £N SELECT Name, Salary FROM Employee WHERE Salary >= 27000 ORDER BY Salary; NameSalary Smith£32,000.00 Chen£31,000.00 Foster£28,000.00 Employee {Empno, Bno, Name, Address, DOB, Position, Salary}

15 Pattern Match – Postcode = EC1 SELECT Name, Postcode FROM Employees WHERE Postcode LIKE 'EC1*'; NamePostcode Caroline CumminsEC1A 7DD Peter LongEC1A 4AB Andrew RawstronEC16 8TY Employee {Empno, Bno, Name, Address, DOB, Position, Salary} * = MS Access % = ISO SQL

16 Range - Between SELECT * FROM Employee WHERE DOB BETWEEN #1/10/2004# AND #31/10/2004#; Employee {Empno, Bno, Name, Address, DOB, Position, Salary}

17 Set Membership - Find all Employees who are Secretaries or Managers SELECT * FROM Employee WHERE Position IN (‘Secretary’, ‘Manager’); SELECT * FROM Employee WHERE Position = ‘Secretary’ OR Position = ‘Manager’; Employee {Empno, Bno, Name, Address, DOB, Position, Salary}

18 Subquery – Lowest Paid Worker SELECT Name, Salary FROM Employee WHERE Salary = (SELECT MIN(Salary) FROM Employee); NameSalary Jones£25,000.00 Employee {Empno, Bno, Name, Address, DOB, Position, Salary}

19 Subqueries Cannot use ORDER BY in subqueries. Cannot perform on multiple attributes. Cannot use LIKE or BETWEEN.

20 SELECT DISTINCT Prevents duplicate tuples appearing in query results - compares whole ROWS only.

21 Simple join queries Example join operation in SQL –SELECT * FROM Employee, TimeCard WHERE Employee.ssn = TimeCard.ssn

22 Outer join queries in Access

23 Queries with multiple operators Example of SQL statement with 3 joins: SELECT lastName, firstName, title, dateRented FROM Movie, Video, PreviousRental, Customer WHERE Movie.movieID = Video.movieID AND Customer.accountID = PreviousRental.accountID AND Video.videoID = PreviousRental.videoID AND dateRented > #01/12/2003#

24 SQL exercises


Download ppt "Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete."

Similar presentations


Ads by Google