Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases SQL Basics Finding the Data You Want Filtering Out or Finding Missing Information

How does SQL know which id to take as we have book_id,patron_id,id in loans table?

select first_name,email from patrons where id =4;

In this query, how does SQL know that it has to select the patron_id only?

4 Answers

Steven Parker
Steven Parker
229,644 Points

The "id" columns all have different names. The column name referenced must match the column name exactly.

The query you listed does not reference "patron_id". It will only look for rows where the column named "id" is equal to 4.

Talha Takleh Omar Takleh
Talha Takleh Omar Takleh
3,082 Points

Remember at the beginning of this course where the instructor mentioned that database consists of data and schema. Schema is the organisation of data where the data is stored and divided up into different sections. Sections are also known as tables and these tables can relate to one another.

For this example we have a number of tables. Loans, patrons and books. Since we want to find the patron and the relationship between the tables for loans and patrons is the id. If you look at the patrons table, it has column named id. That's why on the query we can just use id but must refer to the patrons table.

The above is correct, however, I think he should have looked for id = "5" and not 4. If you look at the table that was pulled it says that the id is 5 and not 4.

If you want to select for patrons_id only. SELECT first_name,email FROM patrons WHERE patrons_id ="4";

If you want to select for id only. SELECT first_name,email FROM patrons WHERE id ="4";

Right now you can see the difference between the 2 and you can choose your correct answer.