The SQL Or clause is used to filter records where at least one of the conditions specified is true. Unlike the AND clause, which requires all conditions to be met, the OR clause allows for flexibility by including records that meet any of the given conditions.
SQL Or Syntax
SELECT column1, column2, ... FROM table_name WHERE condition1 OR condition2 OR ...;
SQL Or Example
SQL Or Basic OR Usage Example
This query retrieves the first_name, last_name, and specialty columns from the DOCTORS table for doctors whose specialty is either ‘Cardiology’ or ‘Neurology’.
SELECT first_name, last_name, specialty FROM Doctors WHERE specialty = 'Cardiology' OR specialty = 'Neurology';
SQL Or Using OR with Multiple conditions Example
This query retrieves the first_name, last_name, dob (date of birth), and gender columns from the PATIENTS table. It includes all female patients (gender = ‘F’) and patients who were born before January 1, 1985 (dob < ‘1985-01-01).
SELECT first_name, last_name, dob, gender FROM Patients WHERE gender = 'F' OR dob < '1985-01-01';
SQL Or Combining OR with AND
This query selects the first_name, last_name, contact_number, and dob (date of birth) columns from the PATIENTS table. It filters the results based on gender (gender = ‘M’), contact_number is not NULL, and dob (date of birth) must be later than January 1, 1990 (dob > ‘1990-01-01’).
SELECT first_name, last_name, contact_number, dob FROM Patients WHERE gender = 'M' AND (contact_number IS NOT NULL OR dob > '1990-01-01');