Lost your password? Please enter your email address. You will receive a link and will create a new password via email.


You must login to ask a question.

You must login to add post.

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

RTSALL Latest Articles

Most commonly used SQL Queries | Condition Vs Case

Most commonly used SQL Queries | Condition Vs Case

SQL Queries with Conditions:

It is essential to take note that the TOP in Microsoft SQL works after the WHERE provision and will return the predefined number of results in the event that they exist anyplace in the table, while ROWNUM fills in as a feature of the WHERE proviso so if other.

There is no such thing as conditions in the predetermined number of columns toward the start of the table, you will obtain zero outcomes when
there could be others to be found.

The basic syntax of SELECT with WHERE clause is:

SELECT column1, column2, columnN
FROM table_name
WHERE [condition]

The [condition] can be any SQL articulation, determined utilizing correlation or intelligent administrators like >, <, =, <>, >=, <=, LIKE, NOT, IN, BETWEEN, and so on.

The following statement returns all columns from the table ‘Cars’ where the status column is ‘READY’:

SELECT * FROM Cars WHERE status = 'READY'

How to Select with CASE in SQL?

When results need to have some logic applied ‘on the fly’ one can use a CASE statement to implement it.

SELECT CASE WHEN Col1 &lt; 50 THEN 'under' ELSE 'over' END threshold
FROM TableName
SELECT
 CASE WHEN Col1 < 50 THEN 'under'
 WHEN Col1 > 50 AND Col1 <100 THEN 'between' 
   ELSE 'over' 
END threshold 
FROM TableName

one also can have CASE inside another CASE statement:

SELECT CASE WHEN Col1 < 50 THEN 'under' ELSE CASE WHEN Col1 > 50 AND Col1 <100 THEN Col1 ELSE 'over' END END threshold
FROM TableName

Read Also: SQL Queries Part-2

Related Posts

Leave a comment

You must login to add a new comment.