0

I am new to sql. How can I write query in postgresql for below statement.

select * 
FROM t1 
where (error_id = (123 or 456 or 789) and message_id = 100) 
   or (error_id = 111 and message_id=222)
6
  • I believe a better place to start your PostgreSQL journey is by going a little through tutorials and the documentation Commented Oct 18, 2022 at 18:06
  • Most likely you need some documentation: postgresql.org/docs/current/index.html Commented Oct 18, 2022 at 18:08
  • You should use the IN keyword when you're checking for multiple values: error_id IN (123 or 456 or 789). Commented Oct 18, 2022 at 18:08
  • @lemon do I need to use a subquery? Commented Oct 18, 2022 at 18:17
  • 1
    @BhanuMittal: Why don't you try to learn SQL instead of random guess work? w3schools.com/sql/sql_where.asp Commented Oct 18, 2022 at 18:20

1 Answer 1

1

Like mentioned in the comments, you should read up on sql and syntax in the postgres documenation. You can also get some practice by using db-fiddle sites, where you can create your own tables, populate them and write queries against them.

The proper query would look like this, where you use IN when including more than one value.

select * 
from t1 
where (error_id in (123, 456, 789) and message_id = 100) 
   or (error_id = 111 and message_id=222)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.