SQL Expressions with Syntax and Examples

We offer you a brighter future with FREE online courses - Start Now!!

We have seen that getting correct data from the database could be problematic sometimes. To avoid and overcome these problems, we prefer to use SQL Expressions.

Expressions are like formulas written within SQL queries. These are used widely for performing mathematical operations on the data stored in our database. We will dive deep into expressions in this tutorial.

What are SQL Expressions?

SQL Expressions are formulas written within SQL queries to perform mathematical operations on the data we have stored in our database.

We have various classifications in SQL expressions as well.

Some of the prominent expressions are numerical and boolean type expressions.

Types of SQL Expressions

SQL expressions are classified into three major divisions, and they are as follows:
1. Numeric Expressions
2. Boolean Expressions
3. Date Expressions

Demo Database

We will be using a database of DataFlair Employees, which contains all the basic details of each employee.

Let us first view our database:
Query: SELECT * FROM DataFlair_Employee ;

 

 

SQL Employee Database

1. SQL Numeric Expressions

We use numerical expressions to perform mathematical operations on the stored data.

Syntax:

SELECT * FROM tableName WHERE condition or expression ;

Example 1: Let us first find employees whose age, if doubled, will be more than 50.
Query: SELECT * FROM dataflair_employee WHERE age*2 > 50 ;

SQL arithmetic expression example

Here we can see the employees whose age would be more than 50 if doubled is displayed.

2. SQL Boolean Expressions

SQL Boolean expressions fetch data based on one-to-one matching. In other words, we can think of it as a query that fetches one result at a time.

Syntax:

SELECT * FROM tableName WHERE boolean condition ;

Example 1: Let us find out employees whose age is equal to 26.
Query: SELECT * FROM dataflair_employee WHERE age = 26 ;

SQL boolean expression example

Here, in the output, we can see the details of the employee with age 26.

3. SQL Date Expressions

SQL date expressions are used to compare and get data according to various date-related queries and conditions.

Syntax:

SELECT * FROM tableName WHERE date condition ;

Example 1: Let us find the employees who were born after 1995 January.
Query: SELECT * FROM dataflair_employee WHERE DoB > DATE(‘1995/01/01’) ;

SQL date expression example

In our output, we can see the details of all the employees born after January 1995.

Summary

Here, we have discussed SQL expressions and their types. Expressions can be used to get the data we require using the query and needed by the user.

When we use SQL expressions, they can be grouped into various types like Numerical, Boolean, and Date type.

After studying these, we can easily obtain data by writing and executing better SQL queries to retrieve data.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

follow dataflair on YouTube

Leave a Reply

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