DbSchema Database Designer

DbSchema Tutorial| SQL AND,OR,NOT OPERATORS

Publish on DbSchema Blog >>>

Structured Query Language (SQL) is an essential tool for individuals working with databases. One of SQL’s most potent features is its ability to filter and sort data using specific criteria. This tutorial will dive deep into the AND, OR, and NOT operators, explaining their roles in data filtering with detailed examples.

Introduction to SQL Filtering

SQL allows us to extract specific datasets from databases. The core strength lies in its filtering capabilities, enabling users to sift through vast tables and pull out only the relevant information. This precision is achieved using conditional operators like AND, OR, and NOT.

Sample Database Table

For our tutorial, let’s work with a fictional Students table. The table schema is as follows:

  • StudentID (INTEGER): A unique identifier for each student.
  • FirstName (TEXT): The first name of the student.
  • Age (INTEGER): Age of the student.
  • Grade (TEXT): The grade obtained by the student.

Sample data from the table:

StudentID FirstName Age Grade
1 Alice 20 A
2 Bob 22 B
3 Charlie 21 A
4 David 20 C
5 Eve 23 B

AND Operator

The AND operator retrieves records meeting all the specified conditions. For a record to be selected, every condition must be true.

Syntax:

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

Example:

To pinpoint students aged 20 with an A grade:

SELECT * FROM Students
WHERE Age=20 AND Grade='A';

Result:

StudentID FirstName Age Grade
1 Alice 20 A

Explanation:

Alice is the sole student aged 20 with a grade ‘A’.

OR Operator

The OR operator is less strict, returning records if any one of the conditions is true.

Syntax:

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

Example:

To list students either aged 20 or having a grade of A:

SELECT * FROM Students
WHERE Age=20 OR Grade='A';

Result:

StudentID FirstName Age Grade
1 Alice 20 A
3 Charlie 21 A
4 David 20 C

Explanation:

Alice and Charlie both achieved grade ‘A’. Among them, Alice and David are 20 years old.

NOT Operator

NOT inverts a condition’s outcome. If a condition is true, NOT renders it false and vice-versa.

Syntax:

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

Example:

To identify students not aged 20:

SELECT * FROM Students
WHERE NOT Age=20;

Result:

StudentID FirstName Age Grade
2 Bob 22 B
3 Charlie 21 A
5 Eve 23 B

Explanation:

Bob, Charlie, and Eve are not 20 years old.

Combining AND, OR, NOT Operators

When using multiple operators, it’s pivotal to use parentheses to ensure the desired order of evaluation.

Syntax:

SELECT column1, column2, ...
FROM table_name
WHERE (condition1 AND condition2) OR (condition3 AND condition4) ...;

Example:

To locate students aged 20 with grade A or those aged 22:

SELECT * FROM Students
WHERE (Age=20 AND Grade='A') OR Age=22;

Result:

StudentID FirstName Age Grade
1 Alice 20 A
2 Bob 22 B

Explanation:

Alice satisfies the first set of conditions, while Bob fulfills the second.

Common Mistakes

  • Order of Evaluation: Ensure you’re aware that AND is evaluated before OR. Using parentheses can prevent confusion.
  • String Quotation: Always enclose string values within single quotes.

Frequently Asked Questions

  • Is there a limit to the number of AND and OR operators in a query?

    • Technically, there’s no set limit. However, for clarity, avoid making your conditions overly complex.
  • How do != and NOT differ?

    • != signifies inequality between values. Conversely, NOT negates a condition.

Practice Questions

  1. Identify students aged 21 or 23.
  2. List students without an A grade.
  3. Find students aged 20 not holding an A grade.
  4. Show students graded B or C, but exclude those aged 23.

By mastering these operators, SQL users can harness the full potential of their databases, ensuring that they always obtain the exact data they need. The beauty of SQL lies in its powerful simplicity, and with practice, these commands will become second nature.

Thank you for following along with this tutorial. Keep practicing and stay tuned for more comprehensive SQL tutorials!

Happy coding!


Remember: Practice makes perfect! The more you use these commands in different combinations, the more comfortable you’ll become with the SQL syntax.

Visually Manage Databases using DbSchema

DbSchema is a databases client and visual designer. DbSchema has a free Community Edition, which can be downloaded here.
DbSchema main features include:

DbSchema Designer alt >

Interactive Diagrams

Design tables, column and foreign keys directly in diagrams, by double-clicking them. Changes will be saved to the design model and, if DbSchema is connected to the database also into the database. More.


Connection Dialog alt >

Simple Connection Dialog

Choose the database location, the user and password, and simply get connected. Choose 'Edit Manually' into the JDBC URL combo to enter a custom URL. More.


Relational Data Explorer alt >

Relational Data Explorer

Explore data from multiple tables simultaneously, using foreign keys or virtual foreign keys. Double-click cells to edit the data. More.


Query Builder alt >

Query Builder

Create SQL Queries featuring JOINS, GROUP BY, ORDER BY just using the mouse. More.


SQL Query Editor alt >

SQL Query Editor

Edit and execute SQL Queries. The editor is autocompletion-enabled. More.


Schema Synchronization alt >

Design Schema in Team & Schema Deployment

DbSchema is using the design model, a copy of the schema structure, independent of the database.
The design model can be saved to file and shared in a team.
Connecting to another database you may compare the design model with the database, commit the differences or merge them in the design model. More.


Dark Theme alt >

Dark Theme

Configurable styles & dark theme. More.


Many features are available in the free Community edition.
The Pro edition adds capabilities to save the design to the model file, design schema in team and deploy the schema on multiple databases.


DbSchema can be downloaded for free. No registration is required.