Can we use IF-ELSE in Advanced SQL Query?

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

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

Thanks.

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 

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?

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

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

You can also use a CASE clause


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

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)