Results 1 to 1 of 1

Thread: Basic SQL Tutorial (DML)

  1. #1
    A12Alex is offline Newbie Net Builder
    Join Date
    Feb 2009
    Queensland, Australia.
    Blog Entries
    Thanked 9 Times in 6 Posts

    Basic SQL Tutorial (DML)

    I'm guessing your interested in learning sql or learning a bit more about sql, well you have come to the right thread. I will be covering the following things: SQL? what can it be used with, inserting data, retrieving data, and that's about it as this is a farly basic tutorial.

    First off, what does sql stand for, what is sql? What can it be used with, and why learn it?

    SQL(Which is a initialism(Type of abbreviation)) Structured Query Language, I'm not going to go into the history because that's not really what this tutorial is about so yeah lol. SQL is a database language(SQL is used as a way of inputting and retrieving data from tables), and it is used with many different types of databases(web based and desktop based), such as mysql, mssql, and PostgreSQL, and as stated before it can be used to input and retrieve data. Why learn sql you ask? simple, I and a heck of a lot of people believe that sql is a must know for any webmaster. I(and this is just my opinion) believe that sql is a very good thing to learn if you plain on learning how things work and how things can be improved on, and it can also be useful in making sites and applications.

    Ok, now for the syntax, and yes I forgot to say before that I will be focusing on the following commands: SELECT, FROM, WHERE and ORDER BY(Pretty much only the DML(Data Manipulation Language) part of SQL), in this tutorial. All(or at least 99.99%) databases using tables, which contain within said tables rows and columns. To select information, simply put we use the SELECT command to do so, and we pick which table by using the FROM(I'm going to use tables with examples to better display what happens).

    The SELECT and FROM syntax is pretty much as followed:

    SELECT ColumnName1, ColumnName2, ...
    FROM TableName
    (Oh and SQL isn't case sensitive... but I like to think it's case sensitive as I prefer to write it out that way)

    Ok, now picture this table:

    Table name : Countries

    Country Area Population Capital 
    Australia 7700000 20000000 Canberra 
    China 9570000 1260000000 Beijing 
    USA 9300000 269000000 Washington DC 
    New Zealand 270000 3800000 Wellington 
    Japan 380000 127000000 Tokyo 
    Singapore 678 3800000 Singapore
    So if you didn't all ready guess, to pull / display two rows(Such as the country and the capital) I would use the following query:
    SELECT Country, Capital
    FROM Countries
    Say I want to pull / display all of the rows, how would I do that? simple just use a *. So it will be like so:
    SELECT * FROM Countries
    and that will be display all rows within the table Countries.

    Now, for the WHERE(I have all ready ruffly covered SELECT and FROM), say I wanted to select all of the information of a Country that had say... Tokyo as it's capital.

    The WHERE syntax is as followed:
    WHERE ColumnName operator value
    Now to do what I wanted to do above(Which was select all of the information of a Country that had Tokyo as it's capital.).

    SELECT *
    FROM Countries
    WHERE Capital = 'Tokyo'
    Simple huh? well it isn't at first as there are a few rules and such for the WHERE command, such as the usage of quotes.

    The following is correct and will work:
    WHERE ColumnName = 'text'
    The following is incorrect and won't work(!!!):

    WHERE ColumnName = text
    BUT! heres the thing, numbers and such are a bit different. numbers don't like quotes around them and they refuse to work if they are.
    The following is correct and will work:
    WHERE ColumnName = 123
    The following is incorrect and won't work(!!!):

    WHERE ColumnName = '123'
    I will come back to the WHERE command a little later on as I have to go soon and I would really like to cover all of the things I wanted to cover within this tutorial now(and post it also lol).

    The order by syntax is as followed:
    The ORDER BY command is used when you want to sort information by a specified column, which can be done by either using ASC(Which is ascending) and DESC(Which is descending). By default results are displayed in ascending order.

    yay two examples, in the first I will list the population in ascending order and in the second I will list the population by descending order, In both examples I will be listing only the Country not the hole row.

    SELECT Country
    FROM Countries
    ORDER BY Population
    SELECT Country
    FROM Countries
    ORDER BY Population DESC
    Well that's it for now, I'll post this and come back to it later and fix up any mistakes(there bound to be some as a man can't remember everything at once :/), and add more to it later.

  2. Thanked by:

    WebEvader (28 March, 2009), Will.Spencer (13 February, 2009)

Similar Threads

  1. Best tutorial site
    By Sbfc_ in forum Community Building
    Replies: 0
    Last Post: 2 March, 2010, 21:56 PM
  2. [WTS] Tutorial Website
    By Marcell Purham in forum Sites
    Replies: 5
    Last Post: 18 December, 2009, 22:03 PM
  3. $150 For A WP Tutorial
    By lethalboom in forum Wordpress
    Replies: 3
    Last Post: 3 July, 2009, 19:36 PM
  4. Replies: 15
    Last Post: 16 January, 2009, 10:58 AM

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts