Library tutorials & articles

SQL In Simple English

Basic SELECT statements

What are you going to discuss now?
Now you will learn basic SQL statements such as SELECT, INSERT, UPDATE and DELETE.
For all the examples in this article we would be using a sample database table which is shown below

Table Name : people

lastname firstname age address city
Pai
Kiran
22
Mahavir Nagar
Mumbai
Hunter
Jason
41
Oak Street
San Jose
Kanetkar
Yashwant
38
Rajabhai Street
Nagpur


What is SELECT statement? How do I use it?
The SELECT statement lets you select a set of values from a table in a database. The values selected from the database table would depend on the various conditions that are specified in the SQL query. Here are some ways to use the SELECT statement. I have listed the SQL statements and the respective results that you would obtain if you would execute those queries.

SELECT lastname,firstname FROM people
Would return a recordset with 3 records. Each record would have 2 values.
The first record would have 2 values 'Pai' and 'Kiran'. Whereas the second record would have 2 values 'Hunter' and 'Jason'.

SELECT * FROM people WHERE firstname='Jason'
Would return a recordset with 1 record. The record would have 5 values in that, namely 'Hunter' , 'Jason' , '41' , 'Oak Street' and 'San Jose'.

SELECT * FROM people WHERE age>25
Would return a recordset with 2 records.

Note : That whenever you are comparing a varchar the value should be enclosed in single inverted commas ( ' ). Whereas when you compare an integer the value need not be enclosed in single inverted commas.


How can I compare a part of the name rather than the entire name?


SELECT * FROM people WHERE firstname LIKE '%an%'
Would return a recordset with 2 records. This statement would return 2 records since the sequence 'an' occurs in 2 firstname values, 'Kiran' and 'Yashwant'.


Can I use Boolean operators such as AND or OR to make complex queries?
Good news!! Yes you can.. Actually as a matter of fact, once you start developing professional database applications you would almost always use such Boolean operators to make effective queries.

SELECT address FROM people WHERE (firstname='Kiran' OR city='Nagpur') AND age>30
Would return a recordset consisting of 1 record with 1 value only. Since AND condition specifies that the firstname of the person could be 'Kiran' or his city could be 'Nagpur' , BUT that person has to be over the age of 30. The recordset would have only 1 value in it : 'Rajabhai Street'.

Comments

  1. 10 Jun 2003 at 14:57

    Hi I would first like to say your site has been really helpful to me.


    When using ASP to display database information in a web page you might use


    [courier new]
    Set adoConnect = Server.CreateObject("ADODB.Connection")
    adoConnect.Open "Reports"
    Set onIndex = Server.CreateObject("ADODB.Recordset")


    strSQL = "SELECT * FROM index WHERE number=10"


    onIndex.Open strSQL, adoConnect
    [/courier new]


    Then if you wanted to display the recordset you could use


    [courier new]
    <% onIndex=("Number") %>
    [/courier new]


    but what I can't work out is if your select statement uses COUNT how do you display the result


    [courier new]
    Set adoConnect = Server.CreateObject("ADODB.Connection")
    adoConnect.Open "Reports"
    Set onIndex = Server.CreateObject("ADODB.Recordset")


    strSQL = "SELECT COUNT(*) FROM index WHERE number=10"


    onIndex.Open strSQL, adoConnect
    [/courier new]

  2. 06 Mar 2002 at 09:17

    sorry probably it has been too long  and may be you've got the answer but try this
    select  * from table where date> #2/2/1979#

  3. 26 Feb 2002 at 12:35

    hi..
    check this ... should work


    select * from tablename where DateOfBirth<TODATE('10/9/1976','mm/dd/yyyy')


    try using TO_DATE with the date you want.. i have used this before in this manner..


    hope it helps


    bye

  4. 26 Feb 2002 at 06:47

    Thanks, very helpful.


    What about dates though?  What is the code for finding dates using the SELECT statement eg 'SELECT from * WHERE DateOfBirth < 10/9/76 10:01:10', I can't get it to work.

  5. 01 Jan 1999 at 00:00

    This thread is for discussions of SQL In Simple English.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related podcasts

  • Stack Overflow: Podcast #31

    This is the thirty-first episode of the StackOverflow podcast, where&#10;Joel and Jeff discuss.. stuff!&#10;&#10;Based on some comments from Podcast #30, we now know that &ldquo;Learning about NP-Completeness from Jeff is like learning about irony from Alanis Morissette&rdquo;. It&rsquo;s funny b...

Related jobs

Events coming up

  • Jun 16

    Code Generation 2009

    Cambridge, United Kingdom

    A developer event with a practical focus on helping people get to grips with code generation tools and technologies.