Skip to content
Advertisement

Getting an output based on conditions and creating a new column and updating it based on a condition in SQL

We have two tables: “riders” and “drivers”. Some riders are also drivers, so will show up on both tables. Each table looks like this:

Table riders

Table riders

I have tried this code:

Write a query that outputs all SF riders and add a column called ‘flag’ that prints ‘true’ if the rider is also a SF driver and ‘false’ if the rider is not an SF driver.

My output should look like a column of user_id and flag

Advertisement

Answer

You are on the right track, but your query has two problems:

  • The definition of flag is just a constant.
  • Your join conditions are not correct.

You need to JOIN the tables on the user_id and then check if there is a match in the second table:

If you want true and false as strings:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement