14
1 SELECT authorID, lastName FROM authors OUTPUT: Exercise AuthorID FirstName LastName 1 Harvey Deitel 2 Paul Deitel 3 Andrew Goldberg 4 David Choffnes

1 SELECT authorID, lastName FROM authors OUTPUT: Exercise AuthorIDFirstNameLastName 1HarveyDeitel 2PaulDeitel 3AndrewGoldberg 4DavidChoffnes

Embed Size (px)

Citation preview

Page 1: 1 SELECT authorID, lastName FROM authors OUTPUT: Exercise AuthorIDFirstNameLastName 1HarveyDeitel 2PaulDeitel 3AndrewGoldberg 4DavidChoffnes

1

SELECT authorID, lastName FROM authors

OUTPUT:

Exercise

AuthorID FirstName LastName

1 Harvey Deitel

2 Paul Deitel

3 Andrew Goldberg

4 David Choffnes

Page 2: 1 SELECT authorID, lastName FROM authors OUTPUT: Exercise AuthorIDFirstNameLastName 1HarveyDeitel 2PaulDeitel 3AndrewGoldberg 4DavidChoffnes

2

SELECT title, editionNumber, copyright

FROM titles

WHERE copyright > '2005'

OUTPUT:

ISBN Title EditionNumber Copyright

0131450913

Internet & World Wide Web How to Program

3 2004

0131525239Visual C# 2005 How to Program

2 2006

0131828274 Operating Systems 3 2004

0131857576 C++ How to Program 5 2005

0131869000Visual Basic 2005 How to Program

3 2006

0132222205 Java How to Program 7 2007

0132404168 C How to Program 5 2007

Page 3: 1 SELECT authorID, lastName FROM authors OUTPUT: Exercise AuthorIDFirstNameLastName 1HarveyDeitel 2PaulDeitel 3AndrewGoldberg 4DavidChoffnes

3

OUTPUT:

SELECT authorID, firstName, lastName

FROM authors

WHERE lastName LIKE 'D%'

authorID firstName lastName

1 Harvey Deitel

2 Paul Deitel

Page 4: 1 SELECT authorID, lastName FROM authors OUTPUT: Exercise AuthorIDFirstNameLastName 1HarveyDeitel 2PaulDeitel 3AndrewGoldberg 4DavidChoffnes

4

SELECT authorID, firstName, lastName

FROM authors

WHERE lastName LIKE '_o%'

OUTPUT:

authorID firstName lastName

3 Andrew Goldberg

Page 5: 1 SELECT authorID, lastName FROM authors OUTPUT: Exercise AuthorIDFirstNameLastName 1HarveyDeitel 2PaulDeitel 3AndrewGoldberg 4DavidChoffnes

5

SELECT authorID, firstName, lastName

FROM authors

ORDER BY lastName ASC

OUTPUT: authorID firstName lastName

4 David Choffnes

1 Harvey Deitel

2 Paul Deitel

3 Andrew Goldberg

Page 6: 1 SELECT authorID, lastName FROM authors OUTPUT: Exercise AuthorIDFirstNameLastName 1HarveyDeitel 2PaulDeitel 3AndrewGoldberg 4DavidChoffnes

6

SELECT authorID, firstName, lastName

FROM authors

ORDER BY lastName DESC

OUTPUT: authorID firstName lastName

3 Andrew Goldberg

1 Harvey Deitel

2 Paul Deitel

4 David Choffnes

Page 7: 1 SELECT authorID, lastName FROM authors OUTPUT: Exercise AuthorIDFirstNameLastName 1HarveyDeitel 2PaulDeitel 3AndrewGoldberg 4DavidChoffnes

7

SELECT authorID, firstName, lastName

FROM authors

ORDER BY lastName, firstName

OUTPUT:

authorID firstName lastName

4 David Choffnes

1 Harvey Deitel

2 Paul Deitel

3 Andrew Goldberg

Page 8: 1 SELECT authorID, lastName FROM authors OUTPUT: Exercise AuthorIDFirstNameLastName 1HarveyDeitel 2PaulDeitel 3AndrewGoldberg 4DavidChoffnes

8

SELECT isbn, title, editionNumber, copyright

FROM titles

WHERE title LIKE '%How to Program'

ORDER BY title ASC

OUTPUT: isbn title editionNumbe

r copyright

0132404168 C How to Program 5 2007

0131857576 C++ How to Program 5 2005

0131450913

Internet & World Wide Web How to Program

3 2004

0132222205 Java How to Program 7 2007

0131869000Visual Basic 2005 How to Program

3 2006

0131525239Visual C# 2005 How to Program

2 2006

Page 9: 1 SELECT authorID, lastName FROM authors OUTPUT: Exercise AuthorIDFirstNameLastName 1HarveyDeitel 2PaulDeitel 3AndrewGoldberg 4DavidChoffnes

9SELECT firstName, lastName, isbn

FROM authors

INNER JOIN authorISBN

ON authors.authorID = authorISBN.authorID

ORDER BY lastName, firstName

OUTPUT: firstName lastName isbn

David Choffnes 0131828274

Harvey Deitel 0131450913

Harvey Deitel 0131525239

Harvey Deitel 0131828274

Harvey Deitel 0131857576

Harvey Deitel 0131869000

Harvey Deitel 0132222205

Harvey Deitel 0132404168

Paul Deitel 0131450913

Paul Deitel 0131525239

Paul Deitel 0131828274

Paul Deitel 0131857576

Paul Deitel 0131869000

Paul Deitel 0132222205

Paul Deitel 0132404168

Andrew Goldberg 0131450913

Page 10: 1 SELECT authorID, lastName FROM authors OUTPUT: Exercise AuthorIDFirstNameLastName 1HarveyDeitel 2PaulDeitel 3AndrewGoldberg 4DavidChoffnes

10

INSERT INTO authors ( firstName, lastName )

VALUES ( 'Sue', 'Smith' )

OUTPUT:

Page 11: 1 SELECT authorID, lastName FROM authors OUTPUT: Exercise AuthorIDFirstNameLastName 1HarveyDeitel 2PaulDeitel 3AndrewGoldberg 4DavidChoffnes

11

UPDATE authors

SET lastName = 'Jones'

WHERE lastName = 'Smith' AND firstName = 'Sue'

OUTPUT:

Page 12: 1 SELECT authorID, lastName FROM authors OUTPUT: Exercise AuthorIDFirstNameLastName 1HarveyDeitel 2PaulDeitel 3AndrewGoldberg 4DavidChoffnes

12

DELETE FROM authors

WHERE lastName = 'Jones' AND firstName = 'Sue'

OUTPUT:

Page 13: 1 SELECT authorID, lastName FROM authors OUTPUT: Exercise AuthorIDFirstNameLastName 1HarveyDeitel 2PaulDeitel 3AndrewGoldberg 4DavidChoffnes

Write SQL queries for the books database that perform each of the following tasks:

a) Select all authors from the Authors table with the columns in the order lastName, firstName and authorID.

b) Select a specific author and list all books for that author. Include the title, year and ISBN number. Order the information alphabetically by title.

c) Add a new author to the Authors table.

d) Add a new title for an author (remember that the book must have an entry in the AuthorISBN table).

Page 14: 1 SELECT authorID, lastName FROM authors OUTPUT: Exercise AuthorIDFirstNameLastName 1HarveyDeitel 2PaulDeitel 3AndrewGoldberg 4DavidChoffnes

Correct each of the following SQL statements that refer to the books database.

a) SELECT FROM author FIELD firstName WHERE authorID = 3

b) SELECT isbn, title FROM Titles ORDER WITH title DESC

c) INSERT Authors ( authorID, firstName, lastName )VALUES ( "2", "Jane", "Doe" )