Loading

SQL SELECT Statement

Submitted by: 
Language: 
Visitors have accessed this post 8360 times.


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

Good practice in SQL (DML statements): http://sql-ex.ru/exercises/

thanks sir for the knowledge that you've shared

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

Add new comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.

Filtered HTML

  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
To prevent automated spam submissions leave this field empty.