Example: Name="Samsung" OR Country="Korea"
Above example, condition can be true one or both.
OR Logical Operator:
Syntax:
Select [*|COLUMN_NAME,...] From [TABLE_NAME] Where [COLUMN_NAME]=['VALUE'] OR [COLUMN_NAME]=['VALUE']
Description:
Select: SQL Keyword.
* | COLUMN_NMME: All or specific column name.
From: SQL Keyword.
TABLE_NAME: Name of table.
Where: SQL keyword.
[OR]: Logical operator.
Here is a Table ItemDetails
ItemCode | ItemName | ManufactureCompany | ItemSerialNo | ItemCategory | ItemQuantity | ItemPrice | ProductCountry |
1 | Samsung Mobile | Samsung | S12345 | Mobile | 500 | 240 | Korea |
2 | Samsung Pro | Samsung | SP1234 | Mobile | 100 | 300 | Korea |
3 | Samsung Headphone | Samsung | SH2345 | Headphone | 50 | 50 | Korea |
4 | iPhone | iPone | iP12345 | Mobile | 1000 | 500 | United Stat |
5 | Apple Mobile | Apple | A12345 | Mobile | 200 | 300 | United Stat |
6 | Apple Computer | Apple | A12343 | Computer | 200 | 400 | United Stat |
7 | Apple Macbook | Apple | AM2345 | Macbook | 50 | 600 | United Stat |
8 | Dell Computer | DELL | D02345 | Computer | 200 | 240 | United Stat |
9 | Dell Laptop | DELL | DL2345 | Laptop | 30 | 240 | United Stat |
10 | Apple Laptop | Apple | AL2345 | Laptop | 500 | 100 | United Stat |
11 | Apple Headphone | Apple | AH2345 | Headphone | 50 | 20 | United Stat |
12 | Sony Camera | Sony | SN2345 | Camera | 200 | 240 | Japan |
13 | Sony Headphone | Sony | SNH345 | Headphone | 500 | 50 | Japan |
14 | Ascer Computer | Ascer | AC2345 | Computer | 100 | 600 | China |
16 | Borother Printer | Brother | B12345 | Printer | 200 | 200 | China |
17 | Ascer Printer | Ascer | AP2345 | Printer | 200 | 150 | China |
18 | Brother Photocopy | Brother | BP2345 | Printer | 20 | 150 | China |
19 | Brother Multi Printer | Brother | BM243 | Photocopy | NULL | NULL | India |
SQL "OR" logical operator example:
Now,
Select ItemCode,ItemName,ManufactureCompany,ItemPrice from ItemDetails Where ManufactureCompany='Samsung' OR ItemPrice='240'
Result:
ItemCode | ItemName | ManufactureCompany | ItemPrice |
1 | Samsung Mobile | Samsung | 240 |
2 | Samsung Pro | Samsung | 300 |
3 | Samsung Headphone | Samsung | 50 |
8 | Dell Computer | DELL | 240 |
9 | Dell Laptop | DELL | 240 |
12 | Sony Camera | Sony | 240 |
Select ItemCode,ItemName,ManufactureCompany,ProductCountry from ItemDetails Where ManufactureCompany='Apple' OR ProductCountry='United Stat'
Result:
ItemCode | ItemName | ManufactureCompany | ProductCountry |
4 | iPhone | iPone | United Stat |
5 | Apple Mobile | Apple | United Stat |
6 | Apple Computer | Apple | United Stat |
7 | Apple Macbook | Apple | United Stat |
8 | Dell Computer | DELL | United Stat |
9 | Dell Laptop | DELL | United Stat |
10 | Apple Laptop | Apple | United Stat |
11 | Apple Headphone | Apple | United Stat |
Select ItemCode,ItemName,ProductCountry,ProductCountry from ItemDetails Where ItemCategory='Mobile' OR ProductCountry='Korea'
Result:
ItemCode | ItemName | ProductCountry | ProductCountry2 |
1 | Samsung Mobile | Korea | Korea |
2 | Samsung Pro | Korea | Korea |
3 | Samsung Headphone | Korea | Korea |
4 | iPhone | United Stat | United Stat |
5 | Apple Mobile | United Stat | United Stat |
Select ItemCode,ItemName,ProductCountry,ItemQuantity from ItemDetails Where ItemCategory='Mobile' OR ItemQuantity>='300'
Result:
ItemCode | ItemName | ProductCountry | ItemQuantity |
1 | Samsung Mobile | Korea | 500 |
2 | Samsung Pro | Korea | 100 |
4 | iPhone | United Stat | 1000 |
5 | Apple Mobile | United Stat | 200 |
10 | Apple Laptop | United Stat | 500 |
13 | Sony Headphone | Japan | 500 |
Select ItemCode,ItemName,ProductCountry,ItemPrice from ItemDetails Where ItemCategory='Laptop' OR ItemPrice<='300'
Result:
ItemCode | ItemName | ProductCountry | ItemPrice |
1 | Samsung Mobile | Korea | 240 |
2 | Samsung Pro | Korea | 300 |
3 | Samsung Headphone | Korea | 50 |
5 | Apple Mobile | United Stat | 300 |
8 | Dell Computer | United Stat | 240 |
9 | Dell Laptop | United Stat | 240 |
10 | Apple Laptop | United Stat | 100 |
11 | Apple Headphone | United Stat | 20 |
12 | Sony Camera | Japan | 240 |
13 | Sony Headphone | Japan | 50 |
16 | Borother Printer | China | 200 |
17 | Ascer Printer | China | 150 |
18 | Brother Photocopy | China | 150 |
Example of OR operator in sql server on Video
Example of OR operator in sql server on Image
[Note: We have created and inserted values into sql table in previous topic. For more details Click here.
OR
Execute the follow command in your sql query window for executing above sql select where statement]
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Insert into ItemDetails values('1','Samsung Mobile','Samsung','12-10-2024','12-01-2050','S12345','Mobile','500','240','Korea','12-10-2025')
Insert into ItemDetails values('2','Samsung Pro','Samsung','12-10-2024','12-01-2050','SP1234','Mobile','100','300','Korea','12-10-2025')
Insert into ItemDetails values('3','Samsung Headphone','Samsung','12-10-2023','12-01-2053','SH2345','Headphone','50','50','Korea','12-10-2025')
Insert into ItemDetails values('4','iPhone','iPone','12-01-2024','12-01-2049','iP12345','Mobile','1000','500','United Stat','12-10-2025')
Insert into ItemDetails values('5','Apple Mobile','Apple','12-11-2024','12-01-2049','A12345','Mobile','200','300','United Stat','12-10-2025')
Insert into ItemDetails values('6','Apple Computer','Apple','12-02-2024','12-01-2050','A12343','Computer','200','400','United Stat','12-10-2025')
Insert into ItemDetails values('7','Apple Macbook','Apple','12-11-2024','12-01-2050','AM2345','Macbook','50','600','United Stat','12-10-2025')
Insert into ItemDetails values('8','Dell Computer','DELL','12-09-2024','12-01-2050','D02345','Computer','200','240','United Stat','12-10-2025')
Insert into ItemDetails values('9','Dell Laptop','DELL','12-09-2024','12-01-2050','DL2345','Laptop','30','240','United Stat','12-10-2025')
Insert into ItemDetails values('10','Apple Laptop','Apple','12-08-2024','12-01-2050','AL2345','Laptop','500','100','United Stat','12-10-2025')
Insert into ItemDetails values('11','Apple Headphone','Apple','12-12-2024','12-01-2050','AH2345','Headphone','50','20','United Stat','12-10-2025')
Insert into ItemDetails values('12','Sony Camera','Sony','12-11-2024','12-11-2050','SN2345','Camera','200','240','Japan','12-11-2025')
Insert into ItemDetails values('13','Sony Headphone','Sony','12-09-2024','12-01-2050','SNH345','Headphone','500','50','Japan','12-11-2025')
Insert into ItemDetails values('14','Ascer Computer','Ascer','12-09-2024','12-07-2050','AC2345','Computer','100','600','China','12-10-2025')
Insert into ItemDetails values('16','Borother Printer','Brother','12-09-2024','12-01-2050','B12345','Printer','200','200','China','12-10-2025')
OR
Execute the follow command in your sql query window for executing above sql select where statement]
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Create Table ItemDetails
(
ItemCode int,
ItemName varchar(30),
ManufactureCompany varchar(50),
ExpiredDate date,
ItemSearialNo varchar (30),
ItemCategory varchar(30),
ItemQuantity int,
ItemPrice float,
ProductCountry varchar(30),
ReceivedDate date,
ItemImage image
)
Insert into ItemDetails values('1','Samsung Mobile','Samsung','12-10-2024','12-01-2050','S12345','Mobile','500','240','Korea','12-10-2025')
Insert into ItemDetails values('2','Samsung Pro','Samsung','12-10-2024','12-01-2050','SP1234','Mobile','100','300','Korea','12-10-2025')
Insert into ItemDetails values('3','Samsung Headphone','Samsung','12-10-2023','12-01-2053','SH2345','Headphone','50','50','Korea','12-10-2025')
Insert into ItemDetails values('4','iPhone','iPone','12-01-2024','12-01-2049','iP12345','Mobile','1000','500','United Stat','12-10-2025')
Insert into ItemDetails values('5','Apple Mobile','Apple','12-11-2024','12-01-2049','A12345','Mobile','200','300','United Stat','12-10-2025')
Insert into ItemDetails values('6','Apple Computer','Apple','12-02-2024','12-01-2050','A12343','Computer','200','400','United Stat','12-10-2025')
Insert into ItemDetails values('7','Apple Macbook','Apple','12-11-2024','12-01-2050','AM2345','Macbook','50','600','United Stat','12-10-2025')
Insert into ItemDetails values('8','Dell Computer','DELL','12-09-2024','12-01-2050','D02345','Computer','200','240','United Stat','12-10-2025')
Insert into ItemDetails values('9','Dell Laptop','DELL','12-09-2024','12-01-2050','DL2345','Laptop','30','240','United Stat','12-10-2025')
Insert into ItemDetails values('10','Apple Laptop','Apple','12-08-2024','12-01-2050','AL2345','Laptop','500','100','United Stat','12-10-2025')
Insert into ItemDetails values('11','Apple Headphone','Apple','12-12-2024','12-01-2050','AH2345','Headphone','50','20','United Stat','12-10-2025')
Insert into ItemDetails values('12','Sony Camera','Sony','12-11-2024','12-11-2050','SN2345','Camera','200','240','Japan','12-11-2025')
Insert into ItemDetails values('13','Sony Headphone','Sony','12-09-2024','12-01-2050','SNH345','Headphone','500','50','Japan','12-11-2025')
Insert into ItemDetails values('14','Ascer Computer','Ascer','12-09-2024','12-07-2050','AC2345','Computer','100','600','China','12-10-2025')
Insert into ItemDetails values('16','Borother Printer','Brother','12-09-2024','12-01-2050','B12345','Printer','200','200','China','12-10-2025')
Insert into ItemDetails values('19','Brother Multi Printer','Brother',GETDATE(),'2050-10-10','BM243','Photocopy',null,null,'India',GETDATE())
Insert into ItemDetails (ItemCode,ItemName,ManufactureCompany,ProductDate,ExpiredDate,ItemSearialNo,ItemCategory,ItemQuantity,ItemPrice,ProductCountry,ReceivedDate) values('17','Ascer Printer','Ascer','12-09-2024','12-07-2050','AP2345','Printer','200','150','China','12-10-2025')
Insert into ItemDetails (ItemCode,ItemName,ManufactureCompany,ProductDate,ExpiredDate,ItemSearialNo,ItemCategory,ItemQuantity,ItemPrice,ProductCountry,ReceivedDate) values('18','Brother Photocopy','Brother','12-08-2024','12-07-2050','BP2345','Printer','20','150','China','12-10-2025')
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Insert into ItemDetails (ItemCode,ItemName,ManufactureCompany,ProductDate,ExpiredDate,ItemSearialNo,ItemCategory,ItemQuantity,ItemPrice,ProductCountry,ReceivedDate) values('18','Brother Photocopy','Brother','12-08-2024','12-07-2050','BP2345','Printer','20','150','China','12-10-2025')
No comments:
Post a Comment