SQL SQL Tutorial SQL Database



SQL And, Or, Not

Structured Query Language (SQL) is a programming language used to manage and manipulate relational databases. It is used to create, modify, and delete databases, tables, and data within those tables. SQL is a powerful language that allows users to retrieve data from multiple tables using logical operators such as And, Or, and Not.

SQL And Operator

The And operator is used to combine two or more conditions in a SQL statement. It returns only those records that meet all the conditions specified in the query. The syntax for using the And operator is as follows:

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

For example, let's say we have a table called "employees" with columns "id", "name", "age", and "salary". We want to retrieve all the employees who are over 30 years old and earn more than $50,000 per year. The SQL query for this would be:

SELECT * FROM employees
WHERE age > 30 AND salary > 50000;

SQL Or Operator

The Or operator is used to combine two or more conditions in a SQL statement. It returns all the records that meet at least one of the conditions specified in the query. The syntax for using the Or operator is as follows:

SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

For example, let's say we have a table called "employees" with columns "id", "name", "age", and "salary". We want to retrieve all the employees who are either over 30 years old or earn more than $50,000 per year. The SQL query for this would be:

SELECT * FROM employees
WHERE age > 30 OR salary > 50000;

SQL Not Operator

The Not operator is used to negate a condition in a SQL statement. It returns all the records that do not meet the specified condition. The syntax for using the Not operator is as follows:

SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

For example, let's say we have a table called "employees" with columns "id", "name", "age", and "salary". We want to retrieve all the employees who are not over 30 years old. The SQL query for this would be:

SELECT * FROM employees
WHERE NOT age > 30;

Conclusion

SQL And, Or, and Not operators are powerful tools that allow users to retrieve data from multiple tables using logical operators. By combining these operators with other SQL commands, users can create complex queries that return only the data they need.

References

Activity