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 < 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
Leave a comment