1565
Views
6
Comments
Can we use IF-ELSE in Advanced SQL Query?
Question

I want to fetch the records from the database based on the condition.

What will be the alternative for IF-ELSE in SQL?

Thanks.

2026-06-05 12-38-17
Eduardo Jauch

Hi Diksha, 

Not sure I understood. 

In SQL we use the WHERE clause to define conditions to filter records. 

And for simple queries, we use aggregates. 

What's your knowledge on SQL? Did you did the online training already? 

Maybe I am not understanding your question. 

Cheers 

UserImage.jpg
Diksha Waghmare

Hi Eduardo,


Consider the following scenario- I have multiple roles so in this case, I want to fetch the records from the database by checking the role i.e. if the role is-'Manager' then want to fetch all records else for other role fetch only those records that match the condition. 

So can I do that by using a single SQL query?

2026-06-05 12-38-17
Eduardo Jauch

Hi, 

Yes, it's perfectly possible, but you don't need an SQL to do that. 

An aggregate will suffice and I always prefer it if not too comicate (simple to maintain and scale app). 

You simply need a filter this type in the aggregate:

CheckManagerRole() or (my conditions go here) 

Cheers

UserImage.jpg
Diksha Waghmare

Eduardo Jauch wrote:

Hi, 

Yes, it's perfectly possible, but you don't need an SQL to do that. 

An aggregate will suffice and I always prefer it if not too comicate (simple to maintain and scale app). 

You simply need a filter this type in the aggregate:

CheckManagerRole() or (my conditions go here) 

Cheers


Thanks

2014-10-21 20-15-17
Alberto Ferreira

You can also use a CASE clause


CASE WHEN <Condition> THEN <True> ELSE <False> END

2017-12-13 08-27-28
Joey Moree

Alberto Ferreira wrote:

You can also use a CASE clause


CASE WHEN <Condition> THEN <True> ELSE <False> END


To improve on this, you can also use IIF (which is a shorthand CASE):

IFF(EXPRESSION, TRUEVALUE, FALSE VALUE)

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.