Thursday, 12 July 2012


SQL WHERE Clause

The WHERE Clause is used when you want to retrieve specific information from a table excluding other irrelevant data. For example, when you want to see the information about students in class 10th only then you do need the information about the students in other class. Retrieving information about all the students would increase the processing time for the query.
So SQL offers a feature called WHERE clause, which we can use to restrict the data that is retrieved. The condition you provide in the WHERE clause filters the rows retrieved from the table and gives you only those rows which you expected to see. WHERE clause can be used along with SELECT, DELETE, UPDATE statements.

Syntax of SQL WHERE Clause:

WHERE {column or expression} comparison-operator value

Syntax for a WHERE clause with Select statement is:

SELECT column_list FROM table-name 
WHERE condition;
  • column or expression - Is the column of a table or a expression
  • comparison-operator - operators like = < > etc.
  • value - Any user value or a column name for comparison
For Example: To find the name of a student with id 100, the query would be like:
SELECT first_name, last_name FROM student_details 
WHERE id = 100;
Comparison Operators and Logical Operators are used in WHERE Clause. These operators are discussed in the next chapter.
NOTE: Aliases defined for the columns in the SELECT statement cannot be used in the WHERE clause to set conditions. Only aliases created for tables can be used to reference the columns in the table.

How to use expressions in the WHERE Clause?

Expressions can also be used in the WHERE clause of the SELECT statement.
For example: Lets consider the employee table. If you want to display employee name, current salary, and a 20% increase in the salary for only those products where the percentage increase in salary is greater than 30000, the SELECT statement can be written as shown below
SELECT name, salary, salary*1.2 AS new_salary FROM employee 
WHERE salary*1.2 > 30000;
Output:
namesalarynew_salary
-------------------------------------
Ahsan3500037000
Zahid3500037000
Tahir30000360000
NOTE: Aliases defined in the SELECT Statement can be used in WHERE Clause.

SQL Alias

SQL Aliases are defined for columns and tables. Basically aliases is created to make the column selected more readable.
For Example: To select the first name of all the students, the query would be like:

Aliases for columns:

SELECT first_name AS Name FROM student_details;
or
SELECT first_name Name FROM student_details;
In the above query, the column first_name is given a alias as 'name'. So when the result is displayed the column name appears as 'Name' instead of 'first_name'.
Output:
Name
-------------

Ahsan Ashfaq
Tahir Rasheed
Agha Zahoor
Nawaab Khan
Zahid Ishaq

Aliases for tables:

SELECT s.first_name FROM student_details s; 
In the above query, alias 's' is defined for the table student_details and the column first_name is selected from the table.
Aliases is more useful when
  • There are more than one tables involved in a query,
  • Functions are used in the query,
  • The column names are big or not readable,
  • More than one columns are combined together

Wednesday, 11 July 2012


SQL SELECT Statement

The most commonly used SQL command is SELECT statement. The SQL SELECT statement is used to query or retrieve data from a table in the database. A query may retrieve information from specified columns or from all of the columns in the table. To create a simple SQL SELECT Statement, you must specify the column(s) name and the table name. The whole query is called SQL SELECT Statement.

Syntax of SQL SELECT Statement:

SELECT column_list FROM table-name 
[WHERE Clause]
[GROUP BY clause]
[HAVING clause]
[ORDER BY clause];
  • table-name is the name of the table from which the information is retrieved.
  • column_list includes one or more columns from which data is retrieved.
  • The code within the brackets is optional.
database table student_details;
idfirst_namelast_nameagesubjectgames
100AhsanAshfaq 10ScienceCricket
101Tahir Rasheed 12MathsFootball
102Agha Zahoor 09ScienceCricket
103Nawaab Khan 18MathsBadminton
104Zahid Ishaq 15EconomicsChess
NOTE: These database tables are used here for better explanation of SQL commands. In reality, the tables can have different columns and different data.
For example, consider the table student_details. To select the first name of all the students the query would be like:
SELECT first_name FROM student_details;
NOTE: The commands are not case sensitive. The above SELECT statement can also be written as "select first_name from students_details;"
You can also retrieve data from more than one column. For example, to select first name and last name of all the students.
SELECT first_name, last_name FROM student_details;
You can also use clauses like WHERE, GROUP BY, HAVING, ORDER BY with SELECT statement. We will discuss these commands in coming chapters.
NOTE: In a SQL SELECT statement only SELECT and FROM statements are mandatory. Other clauses like WHERE, ORDER BY, GROUP BY, HAVING are optional.

How to use expressions in SQL SELECT Statement?

Expressions combine many arithmetic operators, they can be used in SELECT, WHERE and ORDER BY Clauses of the SQL SELECT Statement.
Here we will explain how to use expressions in the SQL SELECT Statement. About using expressions in WHERE and ORDER BY clause, they will be explained in their respective sections.
The operators are evaluated in a specific order of precedence, when more than one arithmetic operator is used in an expression. The order of evaluation is: parentheses, division, multiplication, addition, and subtraction. The evaluation is performed from the left to the right of the expression.
For example: If we want to display the first and last name of an employee combined together, the SQL Select Statement would be like
SELECT first_name || ' ' || last_name FROM employee;
Output:
first_name || ' ' || last_name
---------------------------------
Ahsan Ashfaq
Tahir Rasheed
Agha Zahoor
Nawaab Khan
Zahid Ishaq
You can also provide aliases as below.
SELECT first_name || ' ' || last_name AS emp_name FROM employee;
Output:
emp_name
-------------
Ahsan Ashfaq
Tahir Rasheed
Agha Zahoor
Nawaab Khan
Zahid Ishaq

SQL Operators

There are two type of Operators, namely Comparison Operators and Logical Operators. These operators are used mainly in the WHERE clause, HAVING clause to filter the data to be selected.

Comparison Operators:

Comparison operators are used to compare the column data with specific values in a condition.
Comparison Operators are also used along with the SELECT statement to filter data based on specific conditions.
The below table describes each comparison operator.
Comparison OperatorsDescription
=equal to
<>, !=is not equal to
<less than
>greater than
>=greater than or equal to
<=less than or equal to

Logical Operators:

There are three Logical Operators namely AND, OR and NOT.