Wednesday, April 7, 2010

Database Testing - SQL Lesson 3 - SQL SELECT Statement

Previous12345678910Next

SQL SELECT Statement

SELECT statement is the most commonly used command in SQL. The SELECT statement allows the user to retrieve records/ data or do a query in a table from the database. The query may retrieve information from column or columns from a table. To create a SELECT Statement the column(s) name must be specified and also the table name from which they have to retrieved.


SQL SELECT Statement Syntax


SELECT column1, column2 from tablename
[WHERE condition]
[GROUP BY condition]
[HAVING condition]
[ORDER BY condition];

  • tablename is the name of the table from which the data is retrieved.
  • column1, column2 are the names of the columns from which data is retrieved.
  • The codes in the brackets are optional.

Conditional selections used in the where clause


= Equal
> Greater Than
<>= Greater Than or Equal To
<= Less Than or Equal To <> Not Equal

Sample Database Table - EmployeeDetails


UserIdEmailAddressFirstNameLastNameAgeSalaryCity
1ravi@123.comRaviKumar2020,000Bangalore
2kiran12@12343.comkirankanipaul2535,000Mumbai
3rahul@123.comRahulPatil2825,000Pune
4mirza@123SaniaMirza2350,000Hyderabad
5shoaib@123.comshoaibMalik2850,000Hyderabad
6prisha@123.comPrishaRuth2121,000Delhi


Please Note: The table mentioned is for better understanding and for explanation purpose but the real tables may contain more or less columns. All the examples are based on the table mentioned above.


Example 1

Let us start learning by selecting all fields from the table EmployeeDetails

SELECT * from EmployeeDetails;

When this query is run it will display all the columns of the table.

Example 2: If you want to retrieve data for some fields from the table than selecting all fields using the “*” in select command. You can do this way. S

SELECT EmailAddress, FirstName from EmployeeDetails;


This select statement would return all Email Address and First Names from the EmployeeDetails Table.

You can also use the clauses with SELECT statement like WHERE, GROUP BY, HAVING, ORDER BY. We will be discussing them at a later stage.

Please Note: It is not mandatory to use the WHERE, GROUP BY, HAVING, ORDER BY with the SELECT statement but in a SELECT Statement only SELECT and FROM are mandatory.

Using expressions in an SQL SELECT Statement

As discussed earlier we can use arithmetic operators in the expressions in the SELECT statement using the WHERE, HAVING, ORDER BY clauses. The operators are executed in a defined and set order of precedence where there is more than one arithmetic operator in the statement.

The order of evaluation of Arithmetic operators in an expression is parentheses, division, multiplication, addition, and subtraction. The evaluation starts from the left to the right of the expression.

Example 3

If you want to list out all the employee details whose salary is greater than 30,000 then you will write the query like this.

SELECT * from EmployeeDetails WHERE Salary >30,000;

Other operators will be discussed in the relevant sections.

Previous12345678910Next

No comments:

Post a Comment