SQL
The AND command is used in the WHERE clause of a SQL statement to filter records based on multiple conditions. It ensures that all conditions specified must be met for a row to be included in the result set.
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND ...;
Let’s start by creating a simple database that we’ll use throughout this tutorial. Our database, named “CodingCoursesDB,” consists of two tables: Students
and Courses
.
StudentID | FirstName | LastName | Age |
---|---|---|---|
1 | John | Doe | 22 |
2 | Alice | Smith | 20 |
3 | Bob | Johnson | 23 |
CourseID | CourseName | Instructor |
---|---|---|
101 | Introduction to Programming | Prof. Smith |
102 | Database Management | Prof. Johnson |
103 | Web Development | Prof. Brown |
The SQL AND command allows us to combine multiple conditions in a query. Let’s explore how to use it with practical examples.
SELECT * FROM Students
WHERE Age > 20 AND LastName = 'Johnson';
Output:
StudentID | FirstName | LastName | Age |
---|---|---|---|
3 | Bob | Johnson | 23 |
In this example, we retrieve students who are older than 20 and have the last name ‘Johnson.’