Skip to main content

SQL SELECT Statement

Body

The select command is used to retrieve data from the tables.

Consider the following table:

Users:

Firstname Lastname Age Maritalstatus Country
John Smith 40 Married Usa
Mathew Simon 30 Married UK
Bill Steve 20 Single Usa
Amanda Rogers 28 Married Germany
Steve Hills 30 Single france

Now to get all the data we simply query the database with the select command:

Select * from Users

And the DBMS returns all set of rows in the user table.

The * in the select statement is to select all columns of the table, we can also specify certain columns that we wish to retrieve, all column titles must have to be separated by a comma.

If we want to list only the first name and last name of all users then:

Select Firstname, Lastname from users
Firstname Lastname
John Smith
Mathew Simon
Bill Steve
Amanda Rogers
Steve Hills

We can also filter our choices and dataset on the conditions we specify.

The filtering of data set is made by the where clause, the where clause is used whenever we want some specific data that meets our selection criteria to shown.

Example: we want to list all the user that are from USA:

Select * from Users where Country=USA
Firstname Lastname Age Maritalstatus Country
John Smith 40 Married Usa
Bill Steve 20 Single Usa

Similarly we can also apply our data by filtering on numeric values. For example we want to get all the users who are above or equal to 30 years of age:

Select * from Users where Age >=30
Firstname Lastname Age Maritalstatus Country
John Smith 40 Married Usa
Mathew Simon 30 Married UK
Steve Hills 30 Single france

We can also specify multiple conditions in the where clause.

For example if we want to find the users living in USA and are married:

Select * from Users where county=’USA’ and Maritalstatus=’Married’
Firstname Lastname Age Maritalstatus Country
John Smith 40 Married Usa

Comments

Submitted byAnonymous (not verified)on Sun, 02/27/2011 - 16:03

Good practice in SQL (DML statements): http://sql-ex.ru/exercises/
Submitted byAnonymous (not verified)on Tue, 03/01/2011 - 10:02

In reply to by Anonymous (not verified)

thanks sir for the knowledge that you've shared
Submitted byAnonymous (not verified)on Fri, 03/04/2011 - 20:19

thank you very much sir for this knowledge that u have shared with us !!!!!!!!!!!

Add new comment