Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trouble creating SQL queries – Beginner

I’ve created the linked ERD for some university work but SQL is the one thing that just hasn’t been sticking. I am required to list the id, first name, last name, telephone and address of all members who have made more than one purchase, ensuring I add an appropriate alias when retrieving the details for all members.

ERD link –
[1]: https://i.stack.imgur.com/AD6C0.png

My attempt is below. Is this correct? Can I add individual aliases to each member detail? Can I use "WHERE purchaseTotal" if purchases aren’t a part of the member table?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT (memberId AS 'ID', memberFirstName AS 'First Name', memberLastName AS 'Last Name',
        memberAddress AS 'Address', memberPhone AS 'Phone')
FROM Member
WHERE purchaseTotal > 1;

>Solution :

In very basic terms, you can use only that column in a query, which is present in the tables being queried.

So in your query above, you are using purchaseTotal column, but that is not available in Member table. So you need to add Purchases table as well.

The query should be

SELECT Members.memberId AS 'ID', memberFirstName AS 'First Name', memberLastName AS 'Last Name', memberAddress AS 'Address', memberPhone AS 'Phone'
FROM Purchases
LEFT JOIN Members ON Purchases.memberId = Members.memberId
WHERE purchaseTotal > 1;

Note :

For all the columns that are common in both the tables, you need to specify from which table you need to display the column. Ex, Members.memberId

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading