Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Exercises1 Revising RDB and SQL CTEC2902 Advanced Programming.

Similar presentations


Presentation on theme: "SQL Exercises1 Revising RDB and SQL CTEC2902 Advanced Programming."— Presentation transcript:

1 SQL Exercises1 Revising RDB and SQL CTEC2902 Advanced Programming

2 SQL Exercises2 The story so far... You know How to use existing classes (via their API) You are now ready to tackle ADO.NET, but first... Let’s remember RDB and SQL CTEC2902 Advanced Programming

3 SQL Exercises3 Use this database to answer the following questions Table definitions Books (CopyID, Title, Author, ISBN, PublisherID, Cost) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) e.g. A Simple Library database Books Publishers Borrowers Loans RDB consists of 1 or more tables A table consist of 0 or more records A record consist of 1 or more fields Tables are linked using primary and foreign keys Table name is in bold Primary keys are underlined

4 SQL Exercises4 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g. Extract the name, address, and phone number of all the publishers in the database SELECT Name, Address, Phone FROM Publishers Remember: SELECT always returns a (temporary) table How many columns does the above table have? How many rows?

5 SQL Exercises5 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g. Extract names and addresses of all borrowers on BSc Computing course SELECT Name, Address FROM Borrowers WHERE Course = ‘BSc Computing’ Only those records that satisfy the condition are selected How many columns (or fields) does the above table have? How many rows (or records)?

6 SQL Exercises6 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g. Names of all BIS students with books out SELECT Name FROM Borrowers, Loans WHERE Course = ‘BIS’ AND Loans.BorrowerID = Borrowers.BorrowerID Yes, this SQL will produce duplicate records if a student has borrowed several books Is there a problem with the table that this SQL returns?

7 SQL Exercises7 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g. Names of all BIS students with books out SELECT DISTINCT Name FROM Borrowers, Loans WHERE Course = ‘BIS’ AND Loans.BorrowerID = Borrowers.BorrowerID The keyword DISTINCT will prevent duplicate records occurring in the resulting data table

8 SQL Exercises8 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g. All details of all borrowers registered in the library SELECT * FROM Borrowers The * means “all the fields”

9 SQL Exercises9 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g. All names of all authors, whose names begin with letter K SELECT DISTINCT Author FROM books WHERE Author LIKE ‘K%’ % is called “wildcard”; it means “any string”

10 SQL Exercises10 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g. All books with.NET anywhere in their title SELECT * FROM books WHERE Title LIKE ‘%.NET%’ (The % is a.NET requirement; in other SQL, the & may be required)

11 SQL Exercises11 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g. Names and addresses of all borrowers with books due in on 16/12/2011 SELECT Name, Address FROM Borrowers, Loans WHERE Loans.BorrowerID = Borrowers.BorrowerID AND DueDate = #16/12/2011# Note the # delimiters; they enclose literal date values

12 SQL Exercises12 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g. All details of all borrowers registered in the library, in ascending order of name SELECT * FROM borrowers ORDER BY Name Default is Ascending or ASC e.g., All books in descending order of title SELECT * FROM books ORDER BY Title DESC

13 SQL Exercises13 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g., Fetch the titles of those books that are currently on loan SELECT Title FROM books, loans WHERE books.CopyID = loans.CopyID

14 SQL Exercises14 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g., Fetch the names of all Computing students with books out SELECT Name FROM borrowers, loans WHERE Course = ‘Computing’ AND loans.BorrowerID = borrowers.BorrowerID

15 SQL Exercises15 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g., Get the ISBN of the book, entitled VB.NET Secrets, by Jo Bloggs SELECT ISBN FROM Books WHERE Author = ‘Jo Bloggs’ AND Title = ‘VB.NET Secrets’

16 SQL Exercises16 Questions 1. I want to select two books, one with ID 11 and the other with ID 25. Will this command do the job? SELECT * FROM books WHERE CopyID = 11 AND CopyID = 25 2. Which records will be selected by the following command? SELECT * FROM books WHERE CopyID <> 11 AND CopyID <> 25

17 SQL Exercises17 Extended WHERE Specifying a range of numeric values SELECT * FROM employee WHERE Salary BETWEEN 12000 AND 18000 Q: can similar ranges be specified for strings? Dates? Using sets of values; e.g., SELECT * FROM books WHERE PublisherID IN (3, 4, 5, 6) SELECT * FROM books WHERE PublisherID NOT IN (1, 2)

18 SQL Exercises18 Other SQL commands you will use INSERT INTO UPDATE DELETE Each command has its own, specific syntax Find out from www (or books & notes) Look out for: stored procedures


Download ppt "SQL Exercises1 Revising RDB and SQL CTEC2902 Advanced Programming."

Similar presentations


Ads by Google