The WHERE clause in SQL


In the previous series, Taimienphi.vn introduced you to GROUP BY clause, HAVING clause, Alias ​​(alias) in SQL, today’s article Taimienphi.vn will introduce you more about the WHERE clause in SQL. Please.


The WHERE clause in SQL is used when you want to retrieve specific information from a table, except for irrelevant data.

For example, when looking at the student information panel of A class, you don’t need the student information of other classes. Retrieving information about all students will take longer to process the query.

So SQL provides a clause called WHERE clause that users can use to restrict the data retrieved from the table. The terms and conditions you provide in the WHERE clause will filter irrelevant rows from the table and return only the rows you want to see.

The WHERE clause in SQL is also used along with the SELECT, DELETE, UPDATE statements.

Syntax of the WHERE clause in SQL

WHERE {column or expression} comparison-operator value

Syntax of the WHERE clause with the SELECT statement:

SELECT column_list FROM table-name

WHERE condition;

Inside:

– Column or expression: is the column of a table or an expression.

– Comparison-operator: are operators like =, ….

– Value: Any user values ​​or column names to compare.

Example WHERE clause in SQL

For example, to find the name of a student whose id is 100, the query looks like this:

SELECT first_name, last_name FROM student_details

WHERE id = 100;

Comparison operators and logical operators are also used in the WHERE clause in SQL. Taimienphi.vn will detail this content in the next articles.

Note: Alias ​​(alias) used to define columns in the SELECT statement is not used in the WHERE clause to set conditions. Only aliases created for a table can be used to reference columns in the table.

Use an expression in the WHERE clause in SQL

Expressions can also be used in the WHERE clause of the SELECT statement.

For example, when looking at the company’s employee list. If you want to display the employee name, current salary and a 20% salary increase for some employees with salary higher than 30000, the SELECT statement is written as follows:

SELECT name, salary, salary * 1.2 AS new_salary FROM employee

WHERE salary * 1.2> 30000;

Output:

where in sql 2

NOTE: Aliases defined in the SELECT Statement can be used in WHERE Clause.

Some notes with the WHERE clause in SQL

The aliases defined in the SELECT statement can be used in the WHERE clause.

In addition to the WHERE clause in SQL, there are The ORDER BY clause in SQL, DELETE commands, INSERT statements, … that Taimienphi.vn introduced earlier.

https://thuthuat.taimienphi.vn/where-trong-sql-33281n.aspx
If you are using SQL Server but cannot log in because your SA account is disabled, Taimienphi.vn has instructions on how to Log in to SQL Server If your SA account is disabled for quick fix, resolve this issue. Good luck !

.

Add a Comment

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