+ Reply to Thread
Results 1 to 4 of 4

Thread: SQL: Select Queries

  1. #1
    Join Date
    Mar 2008
    Posts
    7,140
    Rep Power
    86

    SQL: Select Queries

    Writing SELECT queries for SQL databases

    When you want to get results from a database you issue a select query. With this query you can limit the results based on certain criteria, sort it, and subsort it. You can do a lot with this including selecting data from multiple tables. This is done using joins which is a topic for another day.


    First we are going to create a database called test with a people table.

    The code to create our database is:

    Code:
    CREATE DATABASE test;
    Now we need to create a table, and populate it with some data.

    Code:
    CREATE TABLE `test`.`people` (
    `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `name` VARCHAR( 100 ) NOT NULL ,
    `age` SMALLINT NOT NULL
    ) ENGINE = MYISAM ;
    This SQL code creates a table with 3 fields in it. The id field is a unique field that we can use to refer to any records we want. Then we have a name field that is a variable character field. The age field is an integer field to hold a persons age.

    Since this is just a tutorial on select queries, I am going to give you some queries that we can use to demonstrate select queries.

    Code:
    INSERT INTO `people` (`id`, `name`, `age`) VALUES
    (1, 'James', 16),
    (2, 'Joe', 5),
    (3, 'Jack', 9),
    (4, 'Bill', 15),
    (5, 'Topa', 93);
    Selecting all the data

    To select all data we simply use the * to mean select all. Then we name the table.

    The basic syntax is:

    Code:
    SELECT field names | * FROM tableName;
    This code is to select all the data:

    Code:
    SELECT * FROM people;
    The result set will look like this:



    Specifying the Columns

    You can specify what columns you want to return also.

    Example:

    Code:
    SELECT name, age FROM people;
    The resulting output is:




    Where Conditions

    Using the WHERE clause you can limit the results that are returned. You might want to return all names of people who are less than 18? How about all names that start with J? All the math operators like less than, greater than, and so on you can use here.

    Example:

    Code:
    SELECT name, age FROM people WHERE age < 18
    The result is:



    Notice how all the ages are less than 18?

    How about all names that start with J? This is where pattern matching is important.

    Code:
    SELECT name, age FROM people WHERE name LIKE 'J%'
    The result is:



    Notice, how all the names start with J. That is what are where clause says.

    Try this:

    Code:
    SELECT name, age FROM people WHERE name LIKE 'j%'
    Notice, how the result is the same as above? The pattern matching does not depend on case.

    ORDER BY and Sorting

    When we use the ORDER BY clause we can sort our results by any field we want. Let us see how it works when we sort by age. You can also specify if you want to sort in ascending order or in descending order.

    Code:
    SELECT name, age FROM people ORDER BY age DESC;
    Output:



    The code said to select all the names and ages from people and to order it by age in descending order. So the first row is the largest age and so n.

    We can easily sort by ascending order:

    Code:
    SELECT name, age FROM people ORDER BY age ASC;
    The output is:

    Attached Thumbnails Attached Thumbnails SQL: Select Queries-limitresults.jpg   SQL: Select Queries-orderbyasc.jpg   SQL: Select Queries-orderbydesc.jpg   SQL: Select Queries-selectall.jpg   SQL: Select Queries-where1.jpg  

    Attached Images Attached Images  

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: SQL: Select Queries

    Very nice work, Chili5. +rep

  4. #3
    Join Date
    Jul 2006
    Posts
    16,448
    Blog Entries
    74
    Rep Power
    143

    Re: SQL: Select Queries

    Very nice +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    Join Date
    Mar 2009
    Posts
    1,375
    Rep Power
    24

    Re: SQL: Select Queries

    Creating tables from queries is very useful I think. Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to select data of a select list from database table
    By justsachin4u in forum PHP Development
    Replies: 26
    Last Post: 07-10-2011, 12:34 AM
  2. How to Select data of a select list from database???
    By justsachin4u in forum PHP Development
    Replies: 0
    Last Post: 06-25-2011, 04:58 AM
  3. How to Select data of a select list from database???
    By justsachin4u in forum PHP Development
    Replies: 0
    Last Post: 06-25-2011, 04:49 AM
  4. some queries on arrays
    By jackson6612 in forum C and C++
    Replies: 3
    Last Post: 06-22-2011, 03:31 PM
  5. Replies: 4
    Last Post: 08-31-2010, 12:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

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