Click here to Skip to main content
Click here to Skip to main content

SQL in ten minutes

, 31 Mar 2002
Rate this:
Please Sign up or sign in to vote.
Learn SQL basics quickly, as fast as in ten minutes!

Introduction

You've probably heard of SQL. What does it mean, you ask. Well, SQL (Structured Query Language) is a very handy tool for database programmers (including ASP programmers). Without SQL, it would be very difficult to perform many common tasks relating to databases. In this article, I will teach you the basics and the common uses of SQL.

What can it do for me?

SQL is capable of achieving many tasks. Those tasks include:

  • Querying a table. To query a table means we select only the records that we need, according to some criteria we set.
  • Updating a table. Using SQL we can update a group of records that meet certain conditions.
  • Deleting records. SQL allows us to delete records in a breeze, whether it's only one record or a group of records.
  • Adding new records. Well, in this area SQL doesn't help us so much, but it's good to know how to use it for inserting records too.

In the next few paragraphs, you'll learn how exactly these four tasks are accomplished. Let's start!

The SQL statement

A SQL statement is a couple of words that tell the database handler what we want to do. The basic structure of a common SQL statement is:

DIRECTIVE fields FROM/INTO table [WHERE (field OP string) 
                                           [conj] (field OP string)]

Explanation:

  • DIRECTIVE - A SQL directive is a word describing what you want to do.
  • fields - This is a list of the fields on which you are going to execute the SQL statement.
  • FROM/INTO - If you query the table, it's FROM. If you insert a new record, it's INTO.
  • table - On which table we are working.
  • [ ] - Text inside the brackets is optional.
  • conj - This is a logical operator, allows us to make more complex SQL statements.
  • OP - A SQL operator.
  • string - The string you are searching for.

Confusing? Not at all! I will explain each of these weird things in the next paragraphs, along with examples to make you understand it.

The directives

Well, as you can see in the SQL statement structure, every SQL statement must start with a directive. So lets start learning those directives!

SELECT

With the SELECT directive, you can make your queries. Here are some examples:

SELECT * from Workers

The simplest SQL statement. This will select ALL records from the "Workers" table. The asterisk (*) sign means all fields. It will select all records because we have not included a WHERE clause.

SELECT WorkerName from Workers

This will select only the WorkerName field from the table "Workers".

SELECT FirstName,Phone from Phonebook

This statement will select the FirstName and Phone fields of the "Phonebook" table.

OK, so this was really nice. But if I want to show only the people who have got a Mercedes? No problem at all! There are two operators that allows us to query and select only records which meet our criteria. These are IS and LIKE. IS means that we want to select only the records in which a field is exactly what we want. Example :

SELECT * from Phonebook WHERE Car IS 'Mercedes'

This statement will select only the people who own a Mercedes (two people). Note that we don't use quotes in the field and table name, only in the search string. I also used * to select all fields. Note the use of a single quote (') and not double quote("). This is because the SQL statement is a string itself, and strings in programming languages are delimited in double quotes. Another example:

SELECT Car from Phonebook WHERE City IS 'NY'

Here we'll get a table showing the cars of the people living in New York in our phonebook. Well I told you before about IS and LIKE. You know how to use IS already, so let's start using LIKE too. LIKE allows us to define criteria for search. To use LIKE, you will need to know the meaning of a wildcard. A wildcard tells the computer that any other character (or characters) can be placed instead. For example, if the wildcard is @, then saying something like A@ means we search for the names starting with A. But if we say @A, then we mean names ending with A, because the A is after the wildcard. In SQL, the wildcard is not @, but %. Example for the usage of LIKE:

SELECT * from Phonebook WHERE Car LIKE 'M%'

This SQL will get us only the people who own a car brand beginning with "M". Note that if there was a car brand which is named "M" it will be selected too, because the % can be anything, even nothing - that means that there can be some characters after the M, but if there are no more chars its ok too.

SELECT * from Phonebook WHERE FirstName LIKE '%ou%'

This will select only the people who have a first name containing the sequence "ou".

SELECT * from Phonebook WHERE LastName LIKE '%'

This will simply select all records because % means everything.

SELECT * from Phonebook WHERE Car IS 'Mercedes' AND FirstName LIKE '%D%'

Here we used the conj you heard before. It will select for us, only the people who own a Mercedes AND that their first name contains a D.

SELECT * from Workers WHERE ((Department IS 'Sales') OR (Wage > 3000))

Ok - Here we used parenthesis to make reading this statement easier. Also, we used the > operator to select only those workers who work in the "Sales" department OR those workers with a wage of 3000 and higher. You can also use the < and = operators (= is like IS).

DELETE

Well, you know how to query tables. Now to deleting some records. The DELETE directive works exactly the same as SELECT, only it deletes records instead of selecting them. Examples:

DELETE from Workers WHERE FirstName IS 'Moses'

The statement will delete all the workers who are named "Moses". Note that you don't have to write * because you can't delete only certain fields. When you delete a record, or records, all the fields must be deleted. You may, however, put an *, but its not obligatory.

DELETE from Phonebook WHERE City IS 'NY'

Are you nutz!? To delete all your friends from New York!?

DELETE from Phonebook WHERE ((Car IS 'Mercedes') OR (Car IS 'Jaguar'))

Yep, we all feel jealous sometimes for our friends having (very) expensive cars!

UPDATE

So you wanna update some records ha? You've come to the right place! Well, the UPDATE directive is a bit different from SELECT and DELETE directives. This is how you write an UPDATE statement:

UPDATE table SET fields=values
  • table - The table name which we are updating records.
  • fields - The field name, and
  • values - The new values for the fields.

Example:

UPDATE Phonebook SET Lastname='Doe'

This will update all the records in your phonebook, so the last name of all your friends will be "Doe".

UPDATE Phonebook SET Car='None' WHERE 
         ((Car IS 'Ferrari') OR (Car IS 'Lambourghini'))

All your friends with race cars will now have NO car at all! You can also update several fields at once:

UPDATE Phonebook SET FirstName='Bill',LastName='Gates'

This will change all your friends to Bill Gates. It won't, however, make them acquire his budget.

INSERT

This is our last directive. INSERT will help you to add records to your tables. This is how we use it:

INSERT INTO table (fields) VALUES(values)
  • table - What table we are working on
  • fields - The list of fields that we want to give initial values, separated by commas.
  • values - The initial values we want to assign for the fields in the list.

Examples:

INSERT INTO Phonebook (FirstName,LastName) VALUES ('Bill','Gates')

Here we add a new record, and fill in the FirstName and LastName fields of the new record with the values "Bill" and "Gates". The other fields will be empty for now.

INSERT INTO Phonebook (FirstName,Phone) VALUES ('Tim','717-233-344')

I think this explains itself.

To sum up

So, what have we learned in this article? If you've read the whole thing, you now know how to use SQL to:

  • Query your tables.
  • Delete some records from your tables.
  • Update your records.
  • Insert new records.

SQL can do more complicated tasks, but this will give you a good start with SQL. I hope you find this useful!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Dan Pomerchik

Israel Israel
Born in the darkside of the center of Israel (Petah Tiqva city), raised by my two parents, one of them is a computer man.
Started with the basic language, quickly moving on to Pascal than to borland C for dos. Playing for fun with VB and hopefully Visual C++ time will come..

Comments and Discussions

 
Questioncould you tell IS support or not....??? PinmemberS.K.Tripathi10-Aug-13 8:18 
Generalgood intro article PinmemberDonsw21-Feb-09 7:15 
GeneralInclude video in database Pinmember22teddy2227-Mar-07 6:56 
Generalhi PinmemberMember #383788816-Feb-07 21:54 
QuestionHow do i Sum (add) two Values from Diferent tables PinmemberIridania13-Nov-06 5:31 
GeneralGreat article. Thanks! Pinmemberj0xeff15-Feb-06 21:13 
Questionhow to use column numbers in select query PinmemberHiral Patel15-Dec-05 6:35 
GeneralRedbrix's University of Milverfield Pinmemberdamien fulham14-Nov-05 21:15 
GeneralRe: Redbrix's University of Milverfield Pinmemberabhishek_it200525-Jul-06 22:51 
OMG | :OMG: Unsure | :~ Dead | X| Poke tongue | ;-P Laugh | :laugh: Sigh | :sigh: Cry | :(( Sleepy | :zzz: :->
GeneralConverting columns to records PinmemberMark G Hahn12-Oct-05 18:38 
AnswerRe: Converting columns to records PinmemberDinobot_Slag17-Jul-07 6:29 
GeneralAdding records at the top of a table PinsussGiancarlo26-Oct-05 17:48 
GeneralRe: Adding records at the top of a table Pinmemberreal_valo10-Aug-06 3:26 
Questionhow can i copy an object? PinmemberCamilo Sanchez19-Jul-05 13:19 
GeneralSQL server connectivity InC/C++ PinmemberPrabhdeep19-Jun-05 19:48 
GeneralRe: SQL server connectivity InC/C++ Pinmemberalexandros__12-Sep-05 1:54 
Generalasasd PinsussAnonymous9-Apr-05 2:27 
GeneralSQL Help/Example Pinmemberkaritoppinen11-Jun-04 9:35 
Generalproblem in update statement PinsussARATHI VASUPAL29-May-04 18:41 
GeneralRe: problem in update statement PinsussAnonymous23-Dec-04 21:44 
GeneralUPDATE STATEMENT PinsussARATHI VASUPAL29-May-04 18:32 
QuestionHow do I use variables? PinmemberT10006-Jan-04 1:07 
AnswerRe: How do I use variables? PinmemberT10006-Jan-04 3:00 
AnswerRe: How do I use variables? Pinsusswgdesigner23-Jan-04 23:58 
GeneralRe: How do I use variables? Pinmemberreal_valo10-Aug-06 3:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 1 Apr 2002
Article Copyright 2002 by Dan Pomerchik
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid