Home > OS >  How do I apply multiple conditions while filtering in SQL?
How do I apply multiple conditions while filtering in SQL?

Time:08-27

I'm very new to SQL and I couldn't find a similar question. I want to select all the OS versions for Android but I only want to select OS versions 11, 15 or higher for IOS.

OS Type OS version
Android 8
Android 11
IOS 15.1
Other 15.0.1
IOS 11
IOS 14
IOS 15

CodePudding user response:

It's very simple and it's basic SQL syntax with a lot of examples on Stackoverflow (and the internet as well).

Your case:

    SELECT * FROM [your_table] a
    WHERE a.[OS Type] <> 'IOS' 
      OR (a.[OS Type] == 'IOS' 
        AND (a.[OS version] == 11 OR a.[OS version] >= 15))

You can find a lot of examples in SQL on: https://www.w3schools.com/sql/sql_where.asp

CodePudding user response:

You can rephrase your query to:

Select the rows where OStype = 'Android' or OStype = 'IOS' and OSversion = 11 or OStype = 'IOS' and OSversion >= 15

From there it is straightforward to translate to SQL

SELECT OStype, osversion
FROM yourtable
WHERE OStype = 'Android'
   OR OStype = 'IOS' AND OSversion = 11
   OR OStype = 'IOS' and OSversion >= 15

Though not necessary, it is a good idea to use parantheses for clarity when mixing AND, OR predicates

SELECT OStype, osversion
FROM yourtable
WHERE (OStype = 'Android') 
   OR (OStype = 'IOS' AND OSversion = 11) 
   OR (OStype = 'IOS' and OSversion >= 15) 
  • Related