sql and or | And OR Command in SQL - sql - sql tutorial - learn sql




  • The SQL AND & OR operators are used to associate multiple conditions to narrow data in an SQL statement.
  • These operators compare two conditions at a time to determine whether a row can be selected for the output.
  • When retrieving data using a SELECT statement, you can use logical operators in the WHERE clause, which allows you to combine more than one condition.
Logical Operators Description
OR For the row to be selected at least one of the conditions must be true.
AND For a row to be selected all the specified conditions must be true.

"OR" Logical Operator:

  • If you want to select rows that satisfy at least one of the given conditions, you can use the logical operator, OR.

For example:

  • if you want to find the names of students who are studying either Maths or Science, the query would be like,
SELECT first_name, last_name, subject 
FROM student_details 
WHERE subject = 'Maths' OR subject = 'Science'
  • The output would be something like,
first_name last_name Subject
Anajali Bhagwat Maths
Shekar Gowda Maths
Rahul Sharma Science
Stephen Fleming Science
  • The following table describes how logical "OR" operator selects a row.
Column1 Satisfied? Column2 Satisfied? Row Selected
YES YES YES
YES NO YES
NO YES YES
NO NO NO
Tags : sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

"AND" Logical Operator:

  • If you want to select rows that must satisfy all the given conditions, you can use the logical operator, AND.

For Example:

  • To find the names of the students between the age 10 to 15 years, the query would be like:
SELECT first_name, last_name, age 
FROM student_details 
WHERE age >= 10 AND age <= 15;
  • The output would be something like,
first_name last_name age
Rahul Sharma 10
Anajali Bhagwat 12
Shekar Gowda 15
  • The following table describes how logical "AND" operator selects a row.
Column1 Satisfied? Column2 Satisfied? Row Selected
YES YES YES
YES NO YES
NO YES YES
NO NO NO

Interview questions for AND OR COMMAND in SQL:

  • Which of the following SQL statement is valid? (There can be more than one answer
  • a) SELECT First_Name AND Last_Name FROM Users;
  • b) SELECT First_Name, Last_Name FROM Users WHERE Join_Date > 'Apr-01-2015' AND Birth_Date < 'Jan-01-1980';
  • c) SELECT First_Name OR User_Name FROM Users;
  • d) SELECT * FROM Users WHERE Last_Name = 'Brown' AND Gender = 'F';
  • Answer: b),d)
  • How many records will be returned by the following query?
SELECT * FROM Users WHERE Gender = 'M' AND Join_Date = 'Apr-09-2015';

Answer:

First Name Last Name Birth Date Gender Join Date
Jamal Santo Oct-08-1983 M Apr-09-2015
Casey Healy Sep-20-1969 M Apr-09-2015
  • How many records will be returned by the following query?
SELECT * FROM Users WHERE Gender = 'M' OR Join_Date = 'Apr-05-2015';

Answer:

  • 4 records are returned. They are,
First_Name Last_Name Birth_Date Gender Join_Date
Sophie Lee Jan-05-1960 F Apr-05-2015
Richard Brown Jan-07-1975 M Apr-05-2015
Jamal Santo Oct-08-1983 M Apr-09-2015
Casey Healy Sep-20-1969 M Apr-09-2015

This tutorial provides more the basic needs and informations on sql tutorial , pl sql tutorial , mysql tutorial , sql server , sqlcode , sql queries , sql , sql formatter , sql join , w3schools sql , oracle tutorial , mysql , pl sql , learn sql , sql tutorial for beginners , sql server tutorial , sql query tutorial , oracle sql tutorial , t sql tutorial , ms sql tutorial , database tutorial , sql tutorial point , oracle pl sql tutorial , oracle database tutorial , oracle tutorial for beginners , ms sql server tutorial , sql tutorial pdf

Related Searches to And OR Command in SQL