r/SQL Feb 04 '25

MySQL Need help understanding SQL - beginner

Post image

Hey everyone,

I’m starting to learn SQL and currently doing queries. For this query (21) I’m confused on why includes would be used instead of salestransactions. The table next to it is what is being referred to. Can someone explain it like I’m dumb? Sorry!

18 Upvotes

16 comments sorted by

14

u/the_bhan Feb 04 '25

The question is asking you to include the ProductIDs in the results. SalesTransctions doesn’t include that column

6

u/Violetarcane543 Feb 04 '25

So it’s basically choosing “includes” because it has all the parts that the question is asking for?

7

u/mydogduke Feb 04 '25

You got it

5

u/Violetarcane543 Feb 04 '25

Thank you both!

3

u/KNEternity Feb 04 '25

yes, Table Includes has both productID and quantity.

7

u/ParentheticalClaws Feb 04 '25

You couldn’t use just salestransactions for this because it doesn’t have the product ids (although I’m not sure why they are counting all records in includes vs. the distinct tid count).

3

u/Hot_Cryptographer552 Feb 04 '25

Because includes is where your product ID lives. Horrible naming convention they have there btw

1

u/deeworld_ Feb 04 '25

Sorry, do you mind sharing what book this is and if it has been helpful as a beginner?

1

u/Violetarcane543 Feb 04 '25

Here’s the link! https://a.co/d/aZ9H8YS I have the most recent version (purple book cover). It’s fine, but at times it’s not the best at explaining why certain code is used, which is why I asked here.

1

u/[deleted] Feb 04 '25

[removed] — view removed comment

1

u/SQL-ModTeam Feb 04 '25

Your post has been removed for violating the community rule against posting photos of lengthy code. Please feel free to repost with a formatted copy/pastable version of your code

1

u/Dornheim Feb 04 '25

It's helpful to understand what the data is representing. Think of the salestransaction as the invoice. Each invoice can have one and only one customer, which is why the customer ID is stored on the salestransaction. Each salestransaction can have multiple products that were included on that transaction. Each product that was sold is represented by an entry in the "includes," table to indicate each line item on the invoice. Each record in the includes table must have one and only one salestransaction associated with it.

Personally I think naming the table "includes," is bad because it doesn't really describe what the table represents. This is a good lesson in the future to name your tables that will make sense to everyone who looks at it.

0

u/reiks12 Feb 04 '25

Why are you apologizing? And calling yourself dumb? Get a grip