CIS 375—Web App Dev II SQL
Introduction <ul><li>SQL (Structured _______ Language) is an  ANSI  standard language for accessing databases. </li></ul><...
SQL Queries, DML, DDL <ul><li>Suppose a table named  Persons  has fields  SSN ,  LastName ,  FirstName ,  Age ,  Address ,...
SQL: The SELECT Statement <ul><li>The SELECT statement specifies ________ of data to be returned from a table. </li></ul><...
SQL: SELECT DISTINCT <ul><li>A SELECT statement could possibly result in multiple values.  </li></ul><ul><li>Use DISTINCT ...
SQL: The WHERE Clause <ul><li>The WHERE clause specifies _____ of data to be returned from a table. </li></ul><ul><li>Exam...
SQL: INSERT INTO Statement <ul><li>To insert a new row into a table: </li></ul><ul><ul><li>INSERT INTO Persons  VALUES (‘B...
SQL: UPDATE Statement <ul><li>To modify data in an existing row: </li></ul><ul><ul><li>UPDATE Persons SET FirstName = ‘Ric...
SQL: DELETE Statement <ul><li>To delete one or more rows from a table: </li></ul><ul><ul><li>DELETE FROM Persons WHERE Las...
SQL: ORDER BY <ul><li>ORDER BY is used for _________ results of a query. </li></ul><ul><li>Example: </li></ul><ul><ul><li>...
SQL: AND, OR, NOT <ul><li>AND and OR are used to join conditions in a _______ clause. </li></ul><ul><li>Examples: </li></u...
SQL: COUNT Statement <ul><li>To count all rows in a table (‘*’ means no ________ specified): </li></ul><ul><ul><li>SELECT ...
Upcoming SlideShare
Loading in …5
×

Download

494 views
435 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
494
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Download

  1. 1. CIS 375—Web App Dev II SQL
  2. 2. Introduction <ul><li>SQL (Structured _______ Language) is an ANSI standard language for accessing databases. </li></ul><ul><li>SQL can execute queries, retrieve data, insert new records, delete records, and update records in a database. </li></ul><ul><li>SQL works with database programs like MS Access, DB2, Informix, MS SQL Server, Oracle, Sybase, etc. </li></ul><ul><li>A database contains one or more ______. Each table is identified by a name and contains records (rows) and fields (columns). </li></ul><ul><li>Besides W3Schools, you can also try http://www.sqlcourse.com/ (optional). </li></ul>
  3. 3. SQL Queries, DML, DDL <ul><li>Suppose a table named Persons has fields SSN , LastName , FirstName , Age , Address , City and has ten records. </li></ul><ul><li>An SQL query: </li></ul><ul><ul><li>SELECT LastName FROM Persons </li></ul></ul><ul><ul><li>Provides a list of all ten last names in the table </li></ul></ul><ul><li>SQL also has a DML (Data ___________ Language) to update, insert, and delete records. </li></ul><ul><li>SQL also has a DDL (Data __________ Language) to do things like create and delete tables, and to create table indexes (________ keys). </li></ul><ul><li>For example, SSN would be an index in Persons . </li></ul>
  4. 4. SQL: The SELECT Statement <ul><li>The SELECT statement specifies ________ of data to be returned from a table. </li></ul><ul><li>Examples of SQL SELECT statements for the table Persons. </li></ul><ul><ul><li>SELECT LastName, FirstName FROM Persons (use a “,” to separate column names) </li></ul></ul><ul><ul><li>SELECT * FROM Persons (* means “all”) </li></ul></ul><ul><li>Executing an SQL statement produces a ________ set of data. </li></ul><ul><li>Some database programs require a “ ; ” after each SQL statement, but many don’t. </li></ul>
  5. 5. SQL: SELECT DISTINCT <ul><li>A SELECT statement could possibly result in multiple values. </li></ul><ul><li>Use DISTINCT to avoid this possibility. </li></ul><ul><li>Example: </li></ul><ul><ul><li>Suppose you want a list of all distinct cities in the Persons table. </li></ul></ul><ul><ul><li>SELECT DISTINCT City FROM Persons </li></ul></ul><ul><ul><li>Without DISTINCT, the city of Springfield, for example, might appear several times in the list (for each person who lives in Springfield). </li></ul></ul>
  6. 6. SQL: The WHERE Clause <ul><li>The WHERE clause specifies _____ of data to be returned from a table. </li></ul><ul><li>Example of SQL WHERE clause. </li></ul><ul><ul><li>SELECT * FROM Persons WHERE City=‘Springfield’ (returns all fields for records where City is Springfield) </li></ul></ul><ul><ul><li>Use quotes around text, but not around numbers. </li></ul></ul><ul><li>Other operators include <>, >, <, >=, <=, __________, and LIKE. </li></ul><ul><ul><li>SELECT LastName, FirstName FROM Persons WHERE City BETWEEN ‘A’ AND ‘M’ </li></ul></ul><ul><ul><li>SELECT * FROM Persons WHERE LastName LIKE ‘A%’ (returns all last names beginning with A) </li></ul></ul>
  7. 7. SQL: INSERT INTO Statement <ul><li>To insert a new row into a table: </li></ul><ul><ul><li>INSERT INTO Persons  VALUES (‘Bush', ‘George W.', 56, ‘1600 Pennsylvania Ave.', ‘Washington, D.C.') </li></ul></ul><ul><li>To insert only certain values in certain columns of a new row: </li></ul><ul><ul><li>INSERT INTO Persons (LastName, Address) VALUES (‘Hood', ‘Sherwood Forest') </li></ul></ul>
  8. 8. SQL: UPDATE Statement <ul><li>To modify data in an existing row: </li></ul><ul><ul><li>UPDATE Persons SET FirstName = ‘Rick' WHERE LastName = ‘Johnson‘ </li></ul></ul><ul><li>Update several columns in a row: </li></ul><ul><ul><li>UPDATE Persons SET Address = 'Stien 12', City = 'Stavanger' WHERE LastName = 'Rasmussen' </li></ul></ul>
  9. 9. SQL: DELETE Statement <ul><li>To delete one or more rows from a table: </li></ul><ul><ul><li>DELETE FROM Persons WHERE LastName = 'Rasmussen' </li></ul></ul><ul><li>To delete all rows: </li></ul><ul><ul><li>DELETE * FROM Persons </li></ul></ul>
  10. 10. SQL: ORDER BY <ul><li>ORDER BY is used for _________ results of a query. </li></ul><ul><li>Example: </li></ul><ul><ul><li>SELECT LastName, FirstName FROM Persons ORDER BY LastName </li></ul></ul><ul><li>Sort by multiple columns: </li></ul><ul><ul><li>SELECT * FROM Persons ORDER BY LastName, FirstName </li></ul></ul><ul><li>Sort in descending or ascending order: </li></ul><ul><ul><li>SELECT * FROM Persons ORDER BY City DESC, LastName ASC </li></ul></ul><ul><li>You can test your SQL skills with a real database at http://www.w3schools.com/sql/sql_tryit.asp . </li></ul>
  11. 11. SQL: AND, OR, NOT <ul><li>AND and OR are used to join conditions in a _______ clause. </li></ul><ul><li>Examples: </li></ul><ul><ul><li>SELECT * FROM Persons WHERE LastName = ‘Johnson’ AND FirstName = ‘Richard’ </li></ul></ul><ul><ul><li>SELECT * FROM Persons WHERE (FirstName='Tove' OR FirstName='Stephen') AND LastName='Svendson' </li></ul></ul><ul><li>You can also use the keyword _____ with many different kinds of SQL statements: </li></ul><ul><ul><li>SELECT * FROM Persons WHERE NOT (City=‘Berlin' OR City=‘London') </li></ul></ul><ul><ul><li>SELECT LastName, FirstName FROM Persons WHERE City NOT BETWEEN ‘A’ AND ‘M’ </li></ul></ul>
  12. 12. SQL: COUNT Statement <ul><li>To count all rows in a table (‘*’ means no ________ specified): </li></ul><ul><ul><li>SELECT COUNT(*) FROM Persons </li></ul></ul><ul><li>Using COUNT with a WHERE clause: </li></ul><ul><ul><li>SELECT COUNT(*) FROM Persons WHERE Age>20 </li></ul></ul><ul><li>When a column is specified, the null entries are not counted: </li></ul><ul><ul><li>SELECT COUNT(Age) FROM Persons </li></ul></ul><ul><li>To count distinct entries in a particular column: </li></ul><ul><ul><li>SELECT COUNT(DISTINCT City) FROM Persons </li></ul></ul>

×