SQL OR condition

The SQL OR condition is used in a SQL query to create a SQL statement where records are fetching. It can be used in a SELECT statement, INSERT statement, UPDATE statement or DELETE statement. The SQL OR condition requires that one of them conditions should be true.

Let’s see the syntax of OR condition in select statement:

  SELECT columns FROM tables  WHERE condition1  OR condition2;

Let’s see the Example of OR condition in select statement with PHP Script:

<?php
$hostname = 'localhost';
$username = 'root';
$pass = '';
$conn = mysql_connect($hostname, $username, $pass) or die(mysql_error());
mysql_select_db('temp_db') or die(mysql_error()); 
$sql = mysql_query(“SELECT id, name, age, address, salary FROM employee WHERE department=’teacher’ OR age>=’24’”);
?>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
<th>Address</th>
<th>Salary</th>
</tr>       
<?php
while($result = mysql_fetch_assoc($sql))
{
?> 
<tr>
<td><?php echo $result[‘id’];?></td>
<td><?php echo $result[‘name’];?></td>
<td><?php echo $result[‘age’];?></td>
<td><?php echo $result[‘address’];?></td>
<td><?php echo $result[‘salary’];?></td>
</tr>  
<?php
} 
?>
<table>

Output will be look like that :

ID NAME AGE ADDRESS SALARY
1 himesh gupta 24 jaipur 15000
2 Shivani tiwari 23 bikaner 17000
3 puran shing 28 kota 24000
4 Gulshan kumar 15 delhi 18000

Let’s see the Example of OR condition in UPDATE statement:

You can see in example that how an SQL “OR” condition is used in SQL UPDATE statement.

<?php
$hostname = 'localhost';
$username = 'root';
$pass = '';
$conn = mysql_connect($hostname, $username, $pass) or die(mysql_error());
mysql_select_db('temp_db') or die(mysql_error()); 
mysql_query(“UPDATE employee set salary=’30000’ WHERE salary=’24000’ OR age>=’24’”);
?>

Let’s see the Example of OR condition in DELETE statement:

You can see in example that how an SQL “OR” condition is used in SQL DELETE statement

<?php
$hostname = 'localhost';
$username = 'root';
$pass = '';
$conn = mysql_connect($hostname, $username, $pass) or die(mysql_error());
mysql_select_db('temp_db') or die(mysql_error()); 
mysql_query(“DELETE from employee WHERE salary=’24000’ OR age>=’24’”);
?>

About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.