Oracle SQL - Select AND Operator

Introduction

If you use the AND operator, you indicate that each row should evaluate to TRUE for both conditions.

select code, category, duration 
from   courses 
where  category = 'BLD' 
and     duration = 2; 

When the OR operator is used, satisfying either condition will result in a record being returned.

However, when the AND operator is used, all conditions must be satisfied before a record is returned.

Demo

SQL>
SQL> drop table courses;

Table dropped.-- www . jav  a 2s . c o m

SQL> create table courses(
  2  code        VARCHAR2(6)  primary key,
  3  description VARCHAR2(30) not null,
  4  category    CHAR(3)      not null,
  5  duration    NUMBER(2)    not null) ;
SQL> insert into courses values('SQL','Introduction to SQL',         'GEN',4);
SQL> insert into courses values('JSON','Oracle for application users','GEN',1);
SQL> insert into courses values('JAVA','Java for Oracle developers',  'BLD',4);
SQL> insert into courses values('PLS','Introduction to PL/SQL',      'BLD',1);
SQL> insert into courses values('XML','XML for Oracle developers',   'BLD',2);
SQL> insert into courses values('ERM','Data modeling with ERM',      'DSG',3);
SQL> insert into courses values('PMT','Process modeling techniques', 'DSG',1);
SQL> insert into courses values('RSD','Relational system design',    'DEF',2);
SQL> insert into courses values('PRO','Prototyping',                 'DSG',5);
SQL> insert into courses values('GEN','System generation',           'DSG',4);
SQL>
SQL> select code, category, duration
  2  from   courses
  3  where  category = 'BLD'
  4  or     duration = 2;

CODE   | CAT |  DURATION
------ | --- | ---------
JAVA   | BLD |  00004.00
PLS    | BLD |  00001.00
XML    | BLD |  00002.00
RSD    | DEF |  00002.00

SQL>

Related Topics