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?
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.
Microsoft provides a JDBC driver for use with SQL Server and Azure SQL Database, enabling connectivity from any Java application, server, or applet.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With