SQL Expression

An introduction to Database

An SQL expression is a combination of one or more values, operators, and SQL functions that evaluate to a value.SQL Expressions are like formulas and they are written in query language. You can also use them to query the database for specific set of data.

Syntax
Consider the basic syntax of the SELECT statement as follows:

SELECT column1, column2,… columnN  FROM table_name  WHERE [CONDITION|EXPRESSION];

Following are the different type of SQL expressions:

Boolean Expression

SQL Boolean Expressions fetch the data on the basis of matching single value. Following is the syntax:

SELECT column1, column2, columnN FROM table_name WHERE SINGLE VALUE MATCHTING EXPRESSION;

Consider the STUDENT table having the following records:
SQL -> SELECT * FROM STUDENT; ( * means “all” )

IDNAMEAGECOURSECITY
1Ram20BAIndore
2Mukesh22BBAPatna
3Abhay20BEPatna
4Abhinav22MBAAllahabad
5Mimoh22MCAMumbai
6Chirag24MTechJaipur

SQL Numeric Expression:

This expression is used to perform any mathematical operation in any query. Following is the syntax:

SELECT numerical_expression as  OPERATION_NAME FROM table_name WHERE CONDITION] ;

Here numerical_expression is used for mathematical expression or any formula. Following is a simple examples showing usage of SQL Numeric Expressions:
SQL> SELECT (15 + 6) AS ADDITION
+———-+| ADDITION | +———-+|       21 |+———-+

There are several built-in functions like avg(), sum(), count(), etc., to perform what is known as aggregate data calculations against a table or a specific table column.
SQL> SELECT COUNT(*) AS “RECORDS” FROM STUDENT; 

+———+| RECORDS |+———+|       6 +———+

SQL Date Expression:

Date Expressions return current system date and time values:

SQL>  SELECT CURRENT_TIMESTAMP;

+———————+ | Current_Timestamp   | +———————+ | 2015-07-2 09:100:23 | +———————+

Another date expression is as follows:

SQL>  SELECT  GETDATE();