SQL200 SQL Programming Based on  SQL Clearly Explained  by Jan Harrington Module  1 – Relational Database Background, Basi...
Note on SQL200 Slides <ul><li>These slides were originally designed to support the single SQL200 course which was used for...
Warning! <ul><li>Below are some table name changes to be aware of in doing queries. We have created synonyms so either nam...
SQL200 Contact Information Bookstore2 SQL200  Module 2 P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2ass...
SQL200 Resources <ul><li>Bookstore database scripts found on box.net at </li></ul><ul><ul><li>http://tinyurl.com/SQLScript...
SQL Programming <ul><li>Course focus is SQL language </li></ul><ul><li>Widely used for: </li></ul><ul><ul><li>Database adm...
SQL Programming <ul><li>A basic knowledge of query systems, perhaps via MS Access,  or some programming knowledge, is desi...
Relational Database Evolution <ul><li>Based on Codd’s paper </li></ul><ul><li>Early commercial efforts focused on Unix </l...
Relational Database Basics <ul><li>Storage </li></ul><ul><li>Databases </li></ul><ul><li>Tables </li></ul><ul><li>Rows </l...
Bookstore SQL200  Module 1 Relational Database Table
Constraints <ul><li>Database </li></ul><ul><ul><li>Domain </li></ul></ul><ul><ul><li>Uniqueness </li></ul></ul><ul><ul><li...
Bookstore SQL200  Module 1 Relational Database with constraints
Database Management Systems Bookstore SQL200  Module 1 Positioning Chart VLDB Enterprise Workgroup Single user Spreadsheet...
System Architecture Bookstore SQL200  Module 1 Access MDB File Server  Architecture Access
System Architecture Bookstore SQL200  Module 1 Oracle DB Visual Basic App Client/Server  Architecture Access  SQL 
System Architecture Bookstore SQL200  Module 1 Oracle DB Browser Web  Architecture Web Server  SQL 
Approaching SQL <ul><li>Relatively simple </li></ul><ul><li>Two main environments </li></ul><ul><ul><li>Interactive (This ...
SQL Standardization <ul><li>ANSI standardization </li></ul><ul><ul><li>First standard in 1986 </li></ul></ul><ul><ul><li>S...
SQL Conformance <ul><li>Entry </li></ul><ul><li>Intermediate </li></ul><ul><li>Advanced </li></ul><ul><li>Most are at leas...
SQL Statements <ul><li>Data Manipulation Language (DML) </li></ul><ul><li>Data Control Language (DCL) </li></ul><ul><li>Da...
SQL DDL <ul><li>Data definition language (DDL) </li></ul><ul><ul><li>Create, alter, drop, etc. </li></ul></ul><ul><ul><li>...
SQL DCL <ul><li>Data Control Language (DDL) </li></ul><ul><ul><li>Grant </li></ul></ul><ul><ul><li>Revoke </li></ul></ul><...
SQL DML <ul><li>Data Manipulation Language (DML) </li></ul><ul><ul><li>Select </li></ul></ul><ul><ul><li>Insert </li></ul>...
SQL Statement Processing Bookstore SQL200  Module 1 Parse Validate Optimize Access Plan Execute
Bookstore Sample Database <ul><li>Before we continue (note: instructor may have already done this)… </li></ul><ul><li>Load...
Text Conventions <ul><li>In Access character strings are normally surrounded by double quotes </li></ul><ul><ul><li>“ Jone...
Date Conventions <ul><li>In an enterprise database such as Oracle or SQL Sever, enclose dates in single quotes </li></ul><...
SELECT Bookstore SQL200  Module 1 Basic Syntax ( Projection ): Select <column-list> or <*> From <table-list>
SELECT Bookstore SQL200  Module 1 Basic Example ( Projection ): select   customer_last_name,  customer_street from  custom...
MS Access SQL Query Bookstore SQL200  Module 1
Bookstore SQL200  Module 1
SQL Server Query Bookstore SQL200  Module 1
SELECT with Where Clause Bookstore SQL200  Module 1 Example ( Restriction  plus   Projection ): Select <column-list> From ...
Comparison Operators <ul><li>< less than </li></ul><ul><li>> greater than </li></ul><ul><li><= less than or equal to </li>...
SELECT with Where Bookstore SQL200  Module 1 Basic Example ( Restriction plus Projection ): select   customer_last_name,  ...
Select with Where Bookstore SQL200  Module 1
On Your Own <ul><li>Find books written by Mark Twain </li></ul><ul><li>Show title, publisher, year </li></ul>Bookstore SQL...
Complex Predicates Bookstore SQL200  Module 1 Follow normal boolean logic Select   customer_last_name,  customer_street Fr...
Select with Complex Where Bookstore SQL200  Module 1
Complex Where Result Bookstore SQL200  Module 1
Special Operators <ul><li>Can be used in where clause </li></ul><ul><li>LIKE </li></ul><ul><li>IN </li></ul><ul><li>BETWEE...
Like (“Wild Card Matches”) <ul><li>ANSI </li></ul><ul><li>Where customer_last_name like ‘Jo%’ </li></ul><ul><li>Like ‘Jo_’...
IN Bookstore SQL200  Module 1 Select  * From  customers Where  customer_last_name in (‘Rizzo’, ‘Jones’, ‘Garcia’) The list...
SQL Where Clause with IN Bookstore SQL200  Module 1
IS NULL Bookstore SQL200  Module 1 Select  * From  customers Where  customer_street IS NULL  SQL uses three valued logic. ...
On Your Own <ul><li>Find all customers with an address not equal to 4592 Maple Lane </li></ul><ul><li>Was Peter Johnson se...
BETWEEN Bookstore SQL200  Module 1 Select * From orders Where order_date BETWEEN ‘1-Jan-99’ and ’31-Dec-99’ Note: date for...
Where with Between Bookstore SQL200  Module 1
Removing Duplicates Bookstore SQL200  Module 1 Select DISTINCT  customer_city From  customers List once each city in which...
Removing Duplicates Bookstore SQL200  Module 1
Sorting – ORDER BY Bookstore SQL200  Module 1 DESC will sort in descending order Basic syntax : Select <column list> From ...
Sorting – ORDER BY Bookstore SQL200  Module 1 Select  * From  customers Order by  customer_state, customer_city Example: L...
Sorting Results with Order By Bookstore SQL200  Module 1
Selecting Top Records Bookstore SQL200  Module 1 Select Top 5 (or top 25 percent)  Customer_last_name  , contact_zip From ...
SQL Exercises <ul><li>List all books whose publisher name begins with “H” or “T”; sort by title [hint: use LIKE] </li></ul...
Notes Bookstore SQL200  Module 1
Upcoming SlideShare
Loading in …5
×

SQL202.1 Accelerated Introduction to SQL Using SQL Server Module 1

347
-1

Published on

SQL202 Accelerated Introduction to SQL Using Microsoft SQL Server Module 1. Covers relational database concepts, basic select statements, filtering results, special operators, wildcards, sorting, removing duplicates and selecting the top values.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
347
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

SQL202.1 Accelerated Introduction to SQL Using SQL Server Module 1

  1. 1. SQL200 SQL Programming Based on SQL Clearly Explained by Jan Harrington Module 1 – Relational Database Background, Basic Single Table Retrieval Operations Bookstore SQL200 Module 1
  2. 2. Note on SQL200 Slides <ul><li>These slides were originally designed to support the single SQL200 course which was used for any of MS Access, MySQL, Oracle and SQL Server. </li></ul><ul><li>As such you may see here slides developed in any one of the above products. </li></ul><ul><li>We are in the process of migrating the Oracle slides and the MS Access slides out into their own slide sets. The SQL200 slides will cover MySQL and SQL Server which are virtually identical for purposes of this course. </li></ul>Bookstore2 SQL200 Module 2
  3. 3. Warning! <ul><li>Below are some table name changes to be aware of in doing queries. We have created synonyms so either name should work. </li></ul>Bookstore2 SQL200 Module 2 New Name Old Name Orders Order_filled Order_Lines Orderlines
  4. 4. SQL200 Contact Information Bookstore2 SQL200 Module 2 P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com [email_address] Copyright 2001-2011. All rights reserved.
  5. 5. SQL200 Resources <ul><li>Bookstore database scripts found on box.net at </li></ul><ul><ul><li>http://tinyurl.com/SQLScripts </li></ul></ul><ul><li>Slides can be viewed on SlideShare… </li></ul><ul><ul><li>http://www.slideshare.net/OCDatabases </li></ul></ul><ul><li>Follow up questions? </li></ul><ul><ul><li>[email_address] </li></ul></ul>Bookstore SQL212 Module 1
  6. 6. SQL Programming <ul><li>Course focus is SQL language </li></ul><ul><li>Widely used for: </li></ul><ul><ul><li>Database administration </li></ul></ul><ul><ul><li>Enterprise application development </li></ul></ul><ul><ul><li>Data driven web sites </li></ul></ul><ul><li>A foundation skill for eBusiness and almost all major business applications that use relational databases </li></ul>Bookstore SQL200 Module 1
  7. 7. SQL Programming <ul><li>A basic knowledge of query systems, perhaps via MS Access, or some programming knowledge, is desirable </li></ul><ul><li>We will use GUI tools or SQL Plus almost exclusively </li></ul>Bookstore SQL200 Module 1
  8. 8. Relational Database Evolution <ul><li>Based on Codd’s paper </li></ul><ul><li>Early commercial efforts focused on Unix </li></ul><ul><li>First mainframe implementation by IBM - precursor to today’s DB2 </li></ul><ul><li>First PC implementation in early 80’s by Oracle </li></ul>Bookstore SQL200 Module 1
  9. 9. Relational Database Basics <ul><li>Storage </li></ul><ul><li>Databases </li></ul><ul><li>Tables </li></ul><ul><li>Rows </li></ul><ul><li>Columns </li></ul><ul><li>Indexes </li></ul><ul><li>Views </li></ul><ul><li>Cursors </li></ul><ul><li>Application interfaces </li></ul>Bookstore SQL200 Module 1
  10. 10. Bookstore SQL200 Module 1 Relational Database Table
  11. 11. Constraints <ul><li>Database </li></ul><ul><ul><li>Domain </li></ul></ul><ul><ul><li>Uniqueness </li></ul></ul><ul><ul><li>Relationship Cardinality </li></ul></ul><ul><ul><ul><li>1 to 1 </li></ul></ul></ul><ul><ul><ul><li>1 to N </li></ul></ul></ul><ul><li>Other Business Rule </li></ul><ul><ul><li>Triggers </li></ul></ul><ul><ul><li>Stored Procedures </li></ul></ul>Bookstore SQL200 Module 1
  12. 12. Bookstore SQL200 Module 1 Relational Database with constraints
  13. 13. Database Management Systems Bookstore SQL200 Module 1 Positioning Chart VLDB Enterprise Workgroup Single user Spreadsheet # Users Cost
  14. 14. System Architecture Bookstore SQL200 Module 1 Access MDB File Server Architecture Access
  15. 15. System Architecture Bookstore SQL200 Module 1 Oracle DB Visual Basic App Client/Server Architecture Access  SQL 
  16. 16. System Architecture Bookstore SQL200 Module 1 Oracle DB Browser Web Architecture Web Server  SQL 
  17. 17. Approaching SQL <ul><li>Relatively simple </li></ul><ul><li>Two main environments </li></ul><ul><ul><li>Interactive (This course) </li></ul></ul><ul><ul><li>Embedded </li></ul></ul><ul><ul><ul><li>Static (Compiled) </li></ul></ul></ul><ul><ul><ul><li>Dynamic </li></ul></ul></ul>Bookstore SQL200 Module 1
  18. 18. SQL Standardization <ul><li>ANSI standardization </li></ul><ul><ul><li>First standard in 1986 </li></ul></ul><ul><ul><li>SQL 89 </li></ul></ul><ul><ul><li>SQL 92 </li></ul></ul><ul><ul><li>SQL 99 </li></ul></ul><ul><li>Various vendor extensions </li></ul><ul><ul><li>Microsoft/Sybase: T-SQL </li></ul></ul><ul><ul><li>Oracle: PL/SQL </li></ul></ul>Bookstore SQL200 Module 1
  19. 19. SQL Conformance <ul><li>Entry </li></ul><ul><li>Intermediate </li></ul><ul><li>Advanced </li></ul><ul><li>Most are at least entry level </li></ul>Bookstore SQL200 Module 1
  20. 20. SQL Statements <ul><li>Data Manipulation Language (DML) </li></ul><ul><li>Data Control Language (DCL) </li></ul><ul><li>Data Definition Language (DDL) </li></ul><ul><li>Note: SQL 99 changes these to seven types </li></ul>Bookstore SQL200 Module 1
  21. 21. SQL DDL <ul><li>Data definition language (DDL) </li></ul><ul><ul><li>Create, alter, drop, etc. </li></ul></ul><ul><ul><li>Frequently implemented via various CASE tools: Visio, Embarcadero, ERWin, etc. </li></ul></ul><ul><ul><li>But very useful for database administration </li></ul></ul>Bookstore SQL200 Module 1
  22. 22. SQL DCL <ul><li>Data Control Language (DDL) </li></ul><ul><ul><li>Grant </li></ul></ul><ul><ul><li>Revoke </li></ul></ul><ul><ul><li>Deny </li></ul></ul><ul><ul><li>Constraints </li></ul></ul>Bookstore SQL200 Module 1
  23. 23. SQL DML <ul><li>Data Manipulation Language (DML) </li></ul><ul><ul><li>Select </li></ul></ul><ul><ul><li>Insert </li></ul></ul><ul><ul><li>Update </li></ul></ul><ul><ul><li>Delete </li></ul></ul>Bookstore SQL200 Module 1
  24. 24. SQL Statement Processing Bookstore SQL200 Module 1 Parse Validate Optimize Access Plan Execute
  25. 25. Bookstore Sample Database <ul><li>Before we continue (note: instructor may have already done this)… </li></ul><ul><li>Load the sample database if you haven’t already </li></ul><ul><ul><li>Use Access import table feature, or </li></ul></ul><ul><ul><li>Run SQL script, or </li></ul></ul><ul><ul><li>Use Access upsizing wizard </li></ul></ul>Bookstore SQL200 Module 1
  26. 26. Text Conventions <ul><li>In Access character strings are normally surrounded by double quotes </li></ul><ul><ul><li>“ Jones” </li></ul></ul><ul><li>In an enterprise database such as Oracle or SQL Sever enclose text strings in single quotes </li></ul><ul><ul><li>‘ Jones’ </li></ul></ul>Bookstore SQL200 Module 1
  27. 27. Date Conventions <ul><li>In an enterprise database such as Oracle or SQL Sever, enclose dates in single quotes </li></ul><ul><ul><li>‘ 2004-12-23’ MySQL </li></ul></ul><ul><ul><li>’ 12-23-2004’ SQL Server </li></ul></ul><ul><ul><li>’ 23-DEC-04’ Oracle </li></ul></ul>Bookstore SQL200 Module 1
  28. 28. SELECT Bookstore SQL200 Module 1 Basic Syntax ( Projection ): Select <column-list> or <*> From <table-list>
  29. 29. SELECT Bookstore SQL200 Module 1 Basic Example ( Projection ): select customer_last_name, customer_street from customers
  30. 30. MS Access SQL Query Bookstore SQL200 Module 1
  31. 31. Bookstore SQL200 Module 1
  32. 32. SQL Server Query Bookstore SQL200 Module 1
  33. 33. SELECT with Where Clause Bookstore SQL200 Module 1 Example ( Restriction plus Projection ): Select <column-list> From <table-list> Where <selection-criteria>;
  34. 34. Comparison Operators <ul><li>< less than </li></ul><ul><li>> greater than </li></ul><ul><li><= less than or equal to </li></ul><ul><li>>= greater than or equal to </li></ul><ul><li><> or != two forms for not equal </li></ul>Bookstore SQL200 Module 1
  35. 35. SELECT with Where Bookstore SQL200 Module 1 Basic Example ( Restriction plus Projection ): select customer_last_name, customer_street from customers where customer_last_name = ‘Jones’
  36. 36. Select with Where Bookstore SQL200 Module 1
  37. 37. On Your Own <ul><li>Find books written by Mark Twain </li></ul><ul><li>Show title, publisher, year </li></ul>Bookstore SQL200 Module 1
  38. 38. Complex Predicates Bookstore SQL200 Module 1 Follow normal boolean logic Select customer_last_name, customer_street From customers Where (customer_last_name = ‘Jones’ or customer_last_name = ‘Smith’)and customer_state=‘NY’
  39. 39. Select with Complex Where Bookstore SQL200 Module 1
  40. 40. Complex Where Result Bookstore SQL200 Module 1
  41. 41. Special Operators <ul><li>Can be used in where clause </li></ul><ul><li>LIKE </li></ul><ul><li>IN </li></ul><ul><li>BETWEEN </li></ul><ul><li>IS NULL </li></ul>Bookstore SQL200 Module 1
  42. 42. Like (“Wild Card Matches”) <ul><li>ANSI </li></ul><ul><li>Where customer_last_name like ‘Jo%’ </li></ul><ul><li>Like ‘Jo_’ </li></ul><ul><li>Access </li></ul><ul><li>Where customer_last_name like “Jo*” </li></ul><ul><li>Like “Jo?” </li></ul>Bookstore SQL200 Module 1
  43. 43. IN Bookstore SQL200 Module 1 Select * From customers Where customer_last_name in (‘Rizzo’, ‘Jones’, ‘Garcia’) The list in parentheses can be replaced by a subquery. We will study this later.
  44. 44. SQL Where Clause with IN Bookstore SQL200 Module 1
  45. 45. IS NULL Bookstore SQL200 Module 1 Select * From customers Where customer_street IS NULL SQL uses three valued logic. Must use IS NULL to test for unknowns. A null is NOT the same as blank or empty.
  46. 46. On Your Own <ul><li>Find all customers with an address not equal to 4592 Maple Lane </li></ul><ul><li>Was Peter Johnson selected? </li></ul><ul><li>Why or why not? </li></ul>Bookstore SQL200 Module 1
  47. 47. BETWEEN Bookstore SQL200 Module 1 Select * From orders Where order_date BETWEEN ‘1-Jan-99’ and ’31-Dec-99’ Note: date formats vary from product to product.
  48. 48. Where with Between Bookstore SQL200 Module 1
  49. 49. Removing Duplicates Bookstore SQL200 Module 1 Select DISTINCT customer_city From customers List once each city in which there are customers Removes duplicate rows from result set
  50. 50. Removing Duplicates Bookstore SQL200 Module 1
  51. 51. Sorting – ORDER BY Bookstore SQL200 Module 1 DESC will sort in descending order Basic syntax : Select <column list> From <table list> Where <selection criteria> Order by <column list> [DESC]
  52. 52. Sorting – ORDER BY Bookstore SQL200 Module 1 Select * From customers Order by customer_state, customer_city Example: List all records sorted by state, city
  53. 53. Sorting Results with Order By Bookstore SQL200 Module 1
  54. 54. Selecting Top Records Bookstore SQL200 Module 1 Select Top 5 (or top 25 percent) Customer_last_name , contact_zip From customers Order by customer_zip desc ; List largest 5 zips or top 25 % of them…
  55. 55. SQL Exercises <ul><li>List all books whose publisher name begins with “H” or “T”; sort by title [hint: use LIKE] </li></ul><ul><li>List all customers whose last name ends with “S”; sort by state, city, last name </li></ul><ul><li>Find the order numbers of orders with order dates in 1999; sort by order #. [Hint: use BETWEEN] </li></ul><ul><li>Find the order numbers and order dates of all orders with a “2” in column 2 of the credit card #; sort by order date descending </li></ul>Bookstore SQL200 Module 1 [end module]
  56. 56. Notes Bookstore SQL200 Module 1

×