|
The intent of this tutorial is to provide a basic introduction to writing queries so that beginners can begin to extract data from an existing relational database. While there are other SQL commands that can be executed to create tables and indexes or to insert, update and delete data, this tutorial will concentrate only on SQL queries to assist the reader in retrieving pre-existing data through definition and relevant example.
The SQL used in this tutorial is "ANSI" (American National Standards Institute), or standard SQL. No specific database management systems features will be referenced. SQL is a query language that allows access to data residing in relational database management systems (RDBMS), such as Sybase, Oracle, Informix, DB2, Microsoft SQL Server, Access and many others. To retrieve information users execute 'queries' to pull the requested information from the database using criteria that is defined by the user. |
||
| A query, in its simplest form is constructed using the following basic query statements SELECT, FROM, WHERE and ORDER BY. The SELECT clause defines what columns or fields you want to see in your results, the FROM clause defines from what table the columns reside in, the WHERE clause defines any special criteria that must be met in order to be displayed, and finally the ORDER BY clause in which you define the sequence you want to display the results. While the only two query clauses that are required are SELECT and FROM, they are almost always accompanied by the WHERE and ORDER BY clauses to restrict the amount of data retrieved and to present it in an orderly fashion. | ||
Database Tables
Keys
SQL Select Clause
SQL Where Clause
Order By
Compound Conditions
Using IN & BETWEEN
Using LIKE
Joining Tables
Using Aliases & Column Concatenation
Aggregate Functions
Group By
Distinct
For More Information
| FAMILY | ||||||
| FAM_ID | NAME_LAST | SALUTATION | STREET_ADDRESS | CITY | STATE | MAIL_CODE |
| 001 | Samples | John & Mary | 1234 Main Street | Anytown | OH | 40000 |
| 002 | Jones | Bob | 376 North West Street | Somwhere | OK | 60000 |
| 003 | Smith | John & Nancy | 123 Treelined Lane | Overthere | CA | 90000 |
| 004 | Brown | Tom | 123 Elm Street | Anywhere | FL | 20000 |
| INDIVIDUAL | ||||||||
| IND_ID | FAM_ID | NAME_LAST | NAME_FIRST | BIRTH_DT | MARITAL_STATUS | SEX | SALARY | ATTEND_TP |
| 001 | 001 | Samples | John | 1959-06-25 | Married | M | 35000 | MEMBR |
| 002 | 001 | Samples | Mary | 1959-12-31 | Married | F | 60000 | MEMBR |
| 003 | 002 | Jones | Bob | 1960-02-12 | Single | M | 40000 | MEMBR |
| 004 | 003 | Smith | John | 1963-08-23 | Married | M | 105000 | VISIT |
| 005 | 003 | Smith | Nancy | 1962-11-14 | Married | F | 0 | VISIT |
| 006 | 004 | Brown | Tom | 1961-11-11 | Divorced | M | 80000 | MEMBR |
| 007 | 003 | Smith | Susie | 1997-04-28 | Single | F | 0 | UNK |
| ATTENDEE_TYPE | |
| ATTEND_TP | ATTEND_DESC |
| MEMBR | Member |
| VISIT | Visitor |
| UNK | Unknown |
| CLASS | |
| CLASS_ID | CLASS_DESC |
| E101 | English 101 |
| E201 | English 201 |
| S101 | Spanish 101 |
| S201 | Spanish 201 |
| CLASS_HISTORY | ||
| CLASS_ID | CLASS_DT | IND_ID |
| E101 | 1999-03-01 | 001 |
| E101 | 1999-03-01 | 002 |
| E101 | 1999-03-01 | 004 |
| E101 | 1999-03-01 | 005 |
| E101 | 2000-03-01 | 003 |
| E101 | 2000-03-01 | 006 |
| E201 | 2000-09-01 | 002 |
| E201 | 2000-09-01 | 004 |
| E201 | 2000-09-01 | 005 |
| NAME_LAST | SALUTATION | STREET_ADDRESS | CITY | STATE | MAIL_CODE |
| Samples | John & Mary | 1234 Main Street | Anytown | OH | 40000 |
| Jones | Bob | 376 North West Street | Somwhere | OK | 60000 |
| Smith | John & Nancy | 123 Treelined Lane | Overthere | CA | 90000 |
| Brown | Tom | 123 Elm Street | Anywhere | FL | 20000 |
| Relational Operators | |
| = | Equal |
| <> or != (check your manual) | Not Equal |
| < | Less Than |
| > | Greater Than |
| <= | Less Than or Equal To |
| >= | Greater Than or Equal To |
| NAME_LAST | NAME_FIRST |
| Samples | John |
| Samples | Mary |
| Smith | John |
| Smith | Nancy |
| NAME_LAST | NAME_FIRST |
| Samples | Mary |
| Samples | John |
| Smith | Nancy |
| Smith | John |
| NAME_LAST | NAME_FIRST |
| Samples | John |
| Samples | Mary |
| NAME_LAST | NAME_FIRST |
| Jones | Bob |
| Brown | Tom |
| NAME_LAST | NAME_FIRST |
| Samples | John |
| Samples | Mary |
| NAME_LAST | NAME_FIRST |
| Samples | John |
| Samples | Mary |
| Smith | John |
| Smith | Nancy |
| Smith | Susie |
| Name | Street Address | City State Zip |
| Samples, Mary | 1234 Main Street | Anytown, OH 40000 |
| Smith, Nancy | 123 Treelined Lane | Overthere, CA 90000 |
| Smith, Susie | 123 Treelined Lane | Overthere, CA 90000 |
Aggregate functions are functions that summarize results of a query instead of reporting each detail record. This document will discuss the following aggregate functions;
| MIN(SALARY) | MAX(SALARY) |
| 35000 | 80000 |
| Attendee Type | Total Salary | Average Salary |
| MEMBR | 215000 | 53750 |
| VISIT | 105000 | 105000 |
![]() |
SAMS Teach Yourself SQL in 10 Minutes
This is a tutorial-based book, organized into a series of easy-to-follow, 10-minute lessons. These well-targeted lessons teach you in 10 minutes what some books take several hours or days to teach. Instead of dwelling on database theory and relational design, this book takes a very hands-on approach to solving the needs of the majority of SQL users who simply need to interact with their data. You will learn retrieving and sorting data, advanced data filtering, using wildcard filtering, manipulating data, combining queries, using views, creating and using stored procedures, and creating triggers. Amazon UK click here. |
![]() |
SQL: The Complete Reference
Excellent for beginners as well as intermediate level programmers. The material is presented clearly and logically, and explanations and examples are very helpful. Highly recommended to anyone wanting to learn SQL as well as a good reference book. Amazon UK click here. |