SQL Expression

By | October 2, 2021
SQL Expression

When we use two or more than two combinations of values, operators and SQL functions, then it is considered as an expression. We write an expression as a statement or query which act as a formula to show some output. We often use various expressions to query a database.

Expression Syntax

SELECT column_name_1, column_name_2, column_name_N
FROM table_name
WHERE [EXPRESSION];

We can apply expression on Different data types which include:

Vamware
  • Boolean
  • Numeric
  • Date

Boolean Expression

In boolean expression we write expression on the basis of the boolean values 0(true) and 1 (false), and we get only true values as query result.

Syntax

SELECT column_name_1, column_name_2, column_name_N
FROM table_name
WHERE value match [boolean EXPRESSION];

Example

Consider this table as Students:

+----+----------+-----+-------+------+
|Rank| NAME     | AGE | Grade | Marks|
+----+----------+-----+-------+------+
|  1 | Luffy    |  18 | A     |  976 |
|  2 | Naruto   |  16 | A     |  970 |
|  3 | Goku     |  20 | A     |  970 |
|  4 | Zoro     |  26| B      |  890 |
|  5 | Asta     |  16 | B     |  860 |
|  6 | Robin    |  24 |B      |  850 |
|  7 | Gohan    |  18 | C     |  845 |
+----+----------+-----+-------+------+
SELECT * FROM Students WHERE marks = 970;

Output

+----+----------+-----+-----------+----------+
|Rank|  NAME    | AGE | Grade     | Marks    |
+----+----------+-----+-----------+----------+
|  2 | Naruto   |  16 | A         |  970     |
|  3 | Goku     |  20 | A         |  970     |
+----+----------+-----+-----------+----------+

Numeric Expression

We use Numeric Expression to perform the Arithmetic Operations.

Syntax

SELECT Numeric_Expression AS Custom_Column_Name;

Example

SELECT (17+20) AS add;

Output

 +----------+
| add       |
+----------+
|       37 |
+----------+

Date Expression

Date Expression are used to show the time and date related outputs.

Example

select CURRENT_TIMESTAMP;

Output

+---------------------+
| CURRENT_TIMESTAMP   |
+---------------------
| 2020-03-29 13:38:43 |
+---------------------+
1 row in set (0.05 sec)

SQL Expression Quick Summary

  • An expression is a query statement that is a combination of one or more than one value, Operators and SQL Functions.
  • In SQL we have 3 major Expressions, Boolean, Numeric and Date.
  • Boolean Expression deals with the logical and comparison operators where we have results in true and false.
  • Numeric Expressions work with Arithmetic Operators.
  • Date Expression Work with Date and time data types.

People are also reading:

Leave a Reply

Your email address will not be published. Required fields are marked *