Previous
Database Terms - and their Meanings 
Next
WHERE is the new if 
SQL(Structured Query Language) Tutorial
SELECT Statement

SELECT Statement

With those basics out of the way, we can start with the SQL language. First thing to learn is the SELECT command. This command is used to get the data from the database. And its used all the time.

Before we go into the code, let me introduce you to the way I write SQL code. SQL is case insensitive - so select is SELECT is SeLEcT. But I have a certain method, a certain convention, for writing SQL code. SQL keywords and functions will be in UPPER CASE. Fields will be in lower case. Most people will use lower case for table names as well - but I prefer using title case for table name.

OK - back to SELECT. Say we want to get all the information about all the characters in our database...

SELECT * FROM Character

I am sure you have guess this already - but * stands for everything. Or all. Or the whole. Or - oh you get the idea, don't you? Maybe we don't want all the data - we just need the name and IQ - then this would be the query...

SELECT name,iq FROM Character

Now you want to rename the fields - you want to call the name field 'Person' and the iq field 'Intelligence' - this is what you do...

SELECT name AS Person,iq AS Intelligence FROM Character

Try to execute the query - and look at the headers - you will notice the name change.

AS keyword creates an alias for the field and uses that instead of the real name. It might seem pointless now, but it is useful when you are using functions(we will get to that later).

SELECT Query Syntax

This is the general syntax of select statements...

SELECT <field names> FROM <table name>
Previous
Database Terms - and their Meanings 
Next
WHERE is the new if 

Comments

punkin at 03 Mar, 2009 06:02
misspelling found. "SQL in case insensitive" should be "SQL is case insensitive"
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed