Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why or condition is working differently compare with Java and SQL

In Java,

int a = 10, b = 10;

if (a == 10 || b==10)
{
// first condition (a==10) is true, so it wont check further
}

But, in SQL,

select * from my table where a = 10 or b = 10;

--As per my understanding, It should return data only based on a.
--But it returns both entries. 

Why is that?

like image 428
Spike Avatar asked Oct 29 '15 19:10

Spike


People also ask

What makes Java language different from SQL?

Java is a high-level programming language that is preferred by most of the developers to develop different programs that can run on windows. On the other side, SQL is the query language that deals with databases such as update, delete, manage are some features for which programmers use the SQL.

Can you use SQL with Java?

Microsoft provides a JDBC driver for use with SQL Server and Azure SQL Database, enabling connectivity from any Java application, server, or applet.

Is Java is a SQL language?

Java Language Extension is a feature of SQL Server used for executing external Java code. The relational data can be used in the external Java code using the extensibility framework. The Java Language Extension is part of SQL Server Language Extensions.

Is SQL easier than Java?

From my experience, yes in general learning any query language like SQL is lot easier/faster than programming language like Java , Python. Is database a programming language? As I understand database, it is any type of data that needs to be manipulated using programming languages like SQL, Python, etc.


3 Answers

You are describing early termination - this means the second statement is only executed if the answer isn't already known, but it doesn't change the outcome (unless you execute an expression in the second statement).

So a == 10 || b == 10 will result in anything where a, or b is 10 - or where a and b are 10. Or more precisely...

a = 10
b = 10

Or

a = 10
b = 0

Or

a = 0
b = 10

If a happens to be 10, you don't really need to check b - but some languages still do.

like image 178
Fenton Avatar answered Nov 14 '22 22:11

Fenton


This is not a great comparison to make. However, it's not necessarily working any different. If you were to add

if (a == 10 || b==10)
{
// first condition (a==10) is true, so it wont check further
// return a or b
}

It would return every time where a or b is 10. Pretty much the same logic behind the SQL statement.

The problem is with how you're misinterpreting WHERE, this will in fact return every record where a or b is 10.

like image 34
Ceelos Avatar answered Nov 14 '22 23:11

Ceelos


The difference is that in Java we are checking for a conditional logic and not aggregation like SQL is doing.

Java does short-circuiting meaning that if the first operand checked is true, it will not do the other operands.

In SQL the WHERE statements is looking for all a conditions and b conditions independently since you are using or

like image 20
gtgaxiola Avatar answered Nov 14 '22 22:11

gtgaxiola