CodeBlanks Logo Icon CodeBlanks

Listing products

Write the query to list products by price and stock.

Adding products

To add a product to the cart you use INSERT. You enter customer, product and quantity info.

INSERT INTO cart (id, customer_id, product_id, qty)
VALUES (1, 100, 5, 2);

Customer 100 adds 2 of product 5.

The values given with VALUES are inserted into cart as new rows.

Adding products — details

Each cart line is a row. A customer can have many lines; each is added as a separate row.

Practice Adding products

Insert into cart the row id=2, customer_id=100, product_id=7, qty=1.

Starter code

query.sql
-- add to cart
INSERT INTO cart
VALUES

Sample solution

query.sql
INSERT INTO cart (id, customer_id, product_id, qty)
VALUES (2, 100, 7, 1)

Expected output

1 satır eklendi

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Values

Question
INSERT INTO cart (id, customer_id, product_id, qty)
____ (1, 100, 5, 2);
Answer
INSERT INTO cart (id, customer_id, product_id, qty)
VALUES (1, 100, 5, 2);

Explanation: After the column list, VALUES holds the values of the row added to the cart.

2 Which statement adds a cart line?

  • DELETE
  • INSERT Correct answer
  • DROP
  • ALTER

Explanation: INSERT adds a new row.

3 Listing products: match the terms

Term Meaning
INSERT insert
INTO into
VALUES values
SELECT read

All lessons in Beginner