Click here to Skip to main content
15,867,704 members
Articles / Database Development / SQL Server
Article

SQL in ten minutes

Rate me:
Please Sign up or sign in to vote.
4.50/5 (51 votes)
31 Mar 20026 min read 267.9K   92   44
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:

SQL
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:

SQL
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.

SQL
SELECT WorkerName from Workers

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

SQL
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 :

SQL
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:

SQL
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:

SQL
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.

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

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

SQL
SELECT * from Phonebook WHERE LastName LIKE '%'

This will simply select all records because % means everything.

SQL
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.

SQL
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:

SQL
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.

SQL
DELETE from Phonebook WHERE City IS 'NY'

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

SQL
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:

SQL
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:

SQL
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".

SQL
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:

SQL
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:

SQL
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:

SQL
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.

SQL
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


Written By
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

 
GeneralMy vote of 5 Pin
Shiv Rathod28-Feb-21 5:25
Shiv Rathod28-Feb-21 5:25 
QuestionSource code Pin
cdry6-Aug-20 13:55
cdry6-Aug-20 13:55 
SuggestionJoins Pin
Member 1357337712-Dec-17 21:54
Member 1357337712-Dec-17 21:54 
QuestionMessage Closed Pin
24-Nov-15 23:51
professionalJealifted24-Nov-15 23:51 
QuestionTHANK YOU ! Pin
Member 1192923623-Aug-15 5:14
Member 1192923623-Aug-15 5:14 
QuestionGreat article esp. for beginners. Pin
Member 1084119810-Apr-15 5:41
Member 1084119810-Apr-15 5:41 
Questioncould you tell IS support or not....??? Pin
Santosh K. Tripathi10-Aug-13 7:18
professionalSantosh K. Tripathi10-Aug-13 7:18 
Generalgood intro article Pin
Donsw21-Feb-09 6:15
Donsw21-Feb-09 6:15 
GeneralInclude video in database Pin
22teddy2227-Mar-07 5:56
22teddy2227-Mar-07 5:56 
Generalhi Pin
Member 383788816-Feb-07 20:54
Member 383788816-Feb-07 20:54 
QuestionHow do i Sum (add) two Values from Diferent tables Pin
Iridania13-Nov-06 4:31
Iridania13-Nov-06 4:31 
GeneralGreat article. Thanks! Pin
j0xeff15-Feb-06 20:13
j0xeff15-Feb-06 20:13 
Questionhow to use column numbers in select query Pin
Hiral Patel15-Dec-05 5:35
Hiral Patel15-Dec-05 5:35 
GeneralRedbrix's University of Milverfield Pin
damien fulham14-Nov-05 20:15
damien fulham14-Nov-05 20:15 
GeneralRe: Redbrix's University of Milverfield Pin
abhishek_it200525-Jul-06 21:51
abhishek_it200525-Jul-06 21:51 
GeneralConverting columns to records Pin
Member 235493612-Oct-05 17:38
Member 235493612-Oct-05 17:38 
AnswerRe: Converting columns to records Pin
Dinobot_Slag17-Jul-07 5:29
Dinobot_Slag17-Jul-07 5:29 
GeneralAdding records at the top of a table Pin
Giancarlo26-Oct-05 16:48
sussGiancarlo26-Oct-05 16:48 
GeneralRe: Adding records at the top of a table Pin
Daniel Vlasceanu10-Aug-06 2:26
Daniel Vlasceanu10-Aug-06 2:26 
Questionhow can i copy an object? Pin
Camilo Sanchez(old)19-Jul-05 12:19
Camilo Sanchez(old)19-Jul-05 12:19 
GeneralSQL server connectivity InC/C++ Pin
Prabhdeep19-Jun-05 18:48
Prabhdeep19-Jun-05 18:48 
GeneralRe: SQL server connectivity InC/C++ Pin
alexandros__12-Sep-05 0:54
alexandros__12-Sep-05 0:54 
Generalasasd Pin
Anonymous9-Apr-05 1:27
Anonymous9-Apr-05 1:27 
GeneralSQL Help/Example Pin
karitoppinen11-Jun-04 8:35
karitoppinen11-Jun-04 8:35 
Generalproblem in update statement Pin
Member 187904829-May-04 17:41
Member 187904829-May-04 17:41 

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

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