Free Newsletters:
DatabaseDaily
 

SQLCourse
Interactive Online SQL Training

Search Database Journal:

HOME News MS SQL Oracle DB2 Access MySQL PHP SQL Etc Scripts Links Forums DBA Talk
internet.com
SQL Courses
1 What is SQL?
2 Table basics
3 Selecting data
4 Creating tables
5 Inserting into a table
6 Updating records
7 Deleting records
8 Drop a table
9 Advanced Queries
10 Standalone SQL interpreter
11 Advertise on SQLCourse.com
12 Database Links
13 Technology Jobs


internet.commerce
Partner With Us















internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers




Become a Marketplace Partner




Selecting Data

The select statement is used to query the database and retrieve selected data that match the criteria that you specify. Here is the format of a simple select statement:

select "column1"
  [,"column2",etc] 
  from "tablename"
  [where "condition"];
  [] = optional

The column names that follow the select keyword determine which columns will be returned in the results. You can select as many column names that you'd like, or you can use a "*" to select all columns.

The table name that follows the keyword from specifies the table that will be queried to retrieve the desired results.

The where clause (optional) specifies which data values or rows will be returned or displayed, based on the criteria described after the keyword where.

Conditional selections used in the where clause:

= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal to
LIKE *See note below

The LIKE pattern matching operator can also be used in the conditional selection of the where clause. Like is a very powerful operator that allows you to select only rows that are "like" what you specify. The percent sign "%" can be used as a wild card to match any possible character that might appear before or after the characters specified. For example:

select first, last, city
   from empinfo
   where first LIKE 'Er%';

This SQL statement will match any first names that start with 'Er'. Strings must be in single quotes.

Or you can specify,

select first, last
   from empinfo
   where last LIKE '%s';

This statement will match any last names that end in a 's'.

select * from empinfo
   where first = 'Eric';

This will only select rows where the first name equals 'Eric' exactly.

Sample Table: empinfo
firstlastidagecitystate
JohnJones9998045PaysonArizona
MaryJones9998225PaysonArizona
EricEdwards8823232San DiegoCalifornia
Mary AnnEdwards8823332PhoenixArizona
GingerHowell9800242CottonwoodArizona
SebastianSmith9200123Gila BendArizona
GusGray2232235BagdadArizona
Mary AnnMay3232652TucsonArizona
EricaWilliams3232760Show LowArizona
LeroyBrown3238022PinetopArizona
ElroyCleaver3238222GlobeArizona

Enter the following sample select statements in the SQL Interpreter Form at the bottom of this page. Before you press "submit", write down your expected results. Press "submit", and compare the results.

select first, last, city from empinfo; 

select last, city, age from empinfo
       where age > 30; 

select first, last, city, state from empinfo
       where first LIKE 'J%'; 

select * from empinfo; 

select first, last, from empinfo
       where last LIKE '%s'; 

select first, last, age from empinfo
       where last LIKE '%illia%'; 

select * from empinfo where first = 'Eric';

Select statement exercises

Enter select statements to:

  1. Display the first name and age for everyone that's in the table.
  2. Display the first name, last name, and city for everyone that's not from Payson.
  3. Display all columns for everyone that is over 40 years old.
  4. Display the first and last names for everyone whose last name ends in an "ay".
  5. Display all columns for everyone whose first name equals "Mary".
  6. Display all columns for everyone whose first name contains "Mary".

Answers to these exercises

SQL Interpreter


SQL Course Curriculum
<<previous 1 2 3 4 5 6 7 8 9  next>>