CodeBlanks Logo Icon CodeBlanks

Adding to cart

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

query.sql
-- view the cart
SELECT
FROM cart
WHERE

Sample solution

query.sql
SELECT product_id, qty
FROM cart
WHERE customer_id = 100

Expected 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

Question
SELECT product_id, qty FROM cart
WHERE ____ = 100;
Answer
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

All lessons in Beginner