Inquiry about producing 2 tables with different SQL conditions

Hi everyone, I have a question relate to produce 2 tables output with different condition using WHERE, AND and OR statements.

The lesson wants me to produce the result in table but I would like to see the difference when having parenthesis on AND and OR in the WHERE statement.

Screen Link: https://app.dataquest.io/m/252/introduction-to-sql/11/grouping-operators-with-parentheses

My Code:

SELECT Major, Major_category, ShareWomen, Unemployment_rate
INTO Table_1
FROM recent_grads
WHERE (Major_category = 'Engineering'AND ShareWomen > 0.5)
OR Unemployment_rate < 0.051;

SELECT Major, Major_category, ShareWomen, Unemployment_rate
INTO Table_2
FROM recent_grads
WHERE (Major_category = 'Engineering')
AND (ShareWomen > 0.5 OR Unemployment_rate < 0.051);

What I expected to happen:
I am expect to have 2 tables called Table 1 and Table 2. The correct answer is the second set of SQL script.
My question is not directly relate to the output of the lesson but I would like to see what is the difference between the result for this 2 tables where the WHERE statement is having different condition.

What actually happened:
The result pop out an error.

How can I improve my code to produce 2 SQL table to see the different of the SQL statement?

Thank you.

1 Like

Hi @alvinyong19,

Welcome to the Community!

Unfortunately, in the mission screen, if you run these 2 codes one after another, you’ll be able to see only the result of the 2nd query. The best way to see both tables is to work on this excercise on your local computer. For this purpose, you have to download the jobs.db database from that mission screen, open a new Jupyter notebook and connect to this database from there. Then, you can run 2 different code cells, one for each table, and compare the results.

Hi Elena_Kosourova, thank you for your reply.

May I know does using INTO statement is appropriate to assign a name to both tables and it will display 2 tables when run both sets of script together?

Thank you.

You mean INSERT INTO?

Well, but to assign a name (alias) to a table you can use WITH... AS... and then use JOIN to combine both tables. However, I’d rather suggest you to run both tables in different code cells. Since the script here is practically an imitation of one code cell, you’d better do it in a Jupyter notebook.

Thanks for the advice. I will try it out.

1 Like