Adding to cart
- SQL course
- Beginner
- Lesson 44/115
- 5 min
Write the INSERT query to add a product to the cart.
Viewing the cart
To view the cart you use SELECT. You filter one customer's lines.
SELECT product_id, qty
FROM cart
WHERE customer_id = 100;This query returns products and quantities in customer 100's cart.
product_id, qty is selected from cart. WHERE keeps only the rows where customer_id = 100 holds.
Viewing the cart — details
Filter the cart by customer id to see what one person picked. Sum the quantities to find the total count.
Practice Viewing the cart
Select product_id and qty from cart where customer_id=100.
Starter code
-- view the cart
SELECT
FROM cart
WHERESample solution
SELECT product_id, qty
FROM cart
WHERE customer_id = 100Expected output
product_id qty 5 2 7 1
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which statement shows the cart?
- INSERT
- DROP
- SELECT Correct answer
- ALTER
Explanation: You read the cart with SELECT.
2 Filter cart
SELECT product_id, qty FROM cart
WHERE ____ = 100;
SELECT product_id, qty FROM cart
WHERE customer_id = 100;
Explanation: customer_id is the right answer.
3 Adding to cart: match the terms
| Term | Meaning |
|---|---|
SELECT |
read |
FROM |
table |
WHERE |
filter |
INSERT |
insert |