How to correctly write a request?

vitaminchik 486 Reputation points
2023-04-29T20:02:11.56+00:00

Hello. Please tell me how to write the correct request. I created a database and placed 3 tables in them. Now I have to display the data. How can I display rows where there is an order for a product with ID=1 and where the person's age is greater than 30?Can we use a non-clustered index here?

User's image

User's image

SQL Server | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Erland Sommarskog 128.9K Reputation points MVP Volunteer Moderator
    2023-04-29T20:17:12.8366667+00:00

    When you write queries in the sense that you want to get data, indexes are of no interest. When you find that the query is slow, that's when you start think about indexes.

    You query would be a simple join bteween the three tables, adding a WHERE clause for the two conditions. I leave it as an exercise to you to actually write the queries.

    And for the future: Don't post code as images. We can't copy and paste from images. Please always post code as text.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. vitaminchik 486 Reputation points
    2023-04-29T21:55:30.7366667+00:00

    SELECT o.CustomerId, c.FirstName, c.LastName, o.ProdactId,

    p.StockQuantity, p.Price

    FROM Orders AS o

    JOIN Customers AS c

    ON o.CustomerId=c.Id

    JOIN Products AS p

    ON o.ProdactId=p.Id

    WHERE c.Age>20 AND o.ProdactId=1


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.