Expressions in SQL
In the previous lesson, you learned about data types and operators in SQL as well as how to use them. In this article we will learn about expressions in SQL.
What are expressions?
An expression is a combination of one or more values, operators, and SQL functions that evaluate to a value. These SQL expressions are like formulas and they are written in query language. You can use them to query the database for a specific set of data.
Syntax of an SQL expression
Consider the basic syntax of the SELECT statement as follows:
SELECT cot1, cot2, cotN FROM ten_bang WHERE [DIEU_KIEN|BIEU_THUC];
There are different types of SQL expressions, which are mentioned below:
- Boolean
- Number
- Day
Now we will learn about each of these types of expressions in detail.
Boolean expression
Boolean expressions in SQL fetch data based on a combination of a value. The syntax of this expression is as follows:
SELECT cot1, cot2, cotN FROM ten_bang WHERE BIEU_THUC_KET_HOP_GIA_TRI_DON;
See the NHANVIEN table with the following records:
SQL> SELECT * FROM NHANVIEN; +----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Huong | 32 | Da Nang | 2000.00 | | 2 | Khuong | 25 | Da Lat | 1500.00 | | 3 | Quyen | 23 | Ha Noi | 2000.00 | | 4 | Chi | 25 | Hue | 6500.00 | | 5 | Hanh | 27 | Phu Tho | 8500.00 | | 6 | Phuong | 22 | My Tho | 4500.00 | | 7 | Duyen | 24 | Ha Noi | 10000.00 | +----+----------+-----+-----------+----------+ 7 rows in set (0.00 sec)
The table below is an example of using Boolean expressions in SQL:
SQL> SELECT * FROM NHANVIEN WHERE LUONG = 10000; +----+-------+-----+---------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+-------+-----+---------+----------+ | 7 | Duyen | 24 | Ha Noi | 10000.00 | +----+-------+-----+---------+----------+ 1 row in set (0.00 sec)
Arithmetic expressions in SQL
These are expressions used to perform any mathematical operations in queries. The syntax of arithmetic expressions in SQL is as follows:
SELECT BIEU_THUC_SO_HOC as TEN_HOAT_DONG [FROM ten_bang WHERE DIEU_KIEN] ;
Here, BIEU_THUC_SO_HOC is used for a mathematical expression or any formula. The following simple example will show you how to use arithmetic expressions in SQL:
SQL> SELECT (3 + 7) AS ADDITION +----------+ | ADDITION | +----------+ | 10 | +----------+ 1 row in set (0.00 sec)
There are a number of built-in functions in SQL such as avg(), sum(), count(), etc. to perform calculation operations and summarize data for a specific table or column in the SQL database. board.
SQL> SELECT COUNT(*) AS "RECORDS" FROM NHANVIEN; +---------+ | RECORDS | +---------+ | 7 | +---------+ 1 row in set (0.00 sec)
Date expression in SQL
Date expression returns the current system's time and date values.
SQL> SELECT CURRENT_TIMESTAMP; +---------------------+ | Current_Timestamp | +---------------------+ | 2017-11-11 06:40:23 | +---------------------+ 1 row in set (0.00 sec)
Another date expression is written as follows:
SQL> SELECT GETDATE();; +-------------------------+ | GETDATE | +-------------------------+ | 2017-11-12 12:07:18.140 | +-------------------------+ 1 row in set (0.00 sec)
So you have grasped the basic expressions in SQL. From the next lessons we start with SQL commands to work with databases and tables.
You should read it
May be interested
- Regular Expression in PHPregular expression is nothing but a sequence or pattern of characters. they provide the basis for pattern matching.
- It turns out cats also have facial expressions, but not all of us can recognizethe latest research shows that cats often show facial emotions, but it's hard for us to recognize this.
- REPL Terminal in Node.jsrepl is an acronym for read eval print loop (understandably: reading - evaluating - printing - repeating) and it represents the computer environment like the console screen in the linux shell where you can type the command line and the system the system will return the results. node.js also has a repl environment.
- Have you tried Audition dancing with FaceDance Challenge?facedance challenge is an extremely new and exciting audition dance game of vietnamese production team, with the task of performing facial expressions as required by the game to score points.
- How to save frequently used formulas on MathTypemathtype has the ability to save formulas, or expressions for users to use for later times.
- How to enter emoji on Macnot only can you use your iphone to insert these iconic facial expressions into your text messages. you can also do the same thing right on your mac.
- ChatGPT will be able to talk like Scarlett Johansson in the movie Heropenai is releasing a voice assistant feature inspired by the movie her that can read facial expressions and translate spoken language in real time.
- MS Access - Lesson 11: Calculating fieldthe calculation field is a field that receives its information from calculations performed on other fields.
- How to use Emoji on Macnot only can you use your iphone to insert these iconic face expressions into your text messages. you can also do the same thing on a mac.
- What signs show your CPU is about to die?there are some signs that your cpu is having problems, read this article to see if your cpu has one of these expressions.