0
0
SQLquery~20 mins

UPDATE with WHERE condition in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
UPDATE Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output after this UPDATE?

Given the table Employees:

id | name   | salary
---+--------+--------
1  | Alice  | 5000
2  | Bob    | 4500
3  | Carol  | 5500

What will be the salary of Bob after running this query?

UPDATE Employees SET salary = salary + 500 WHERE name = 'Bob';
A5000
B4500
C5500
D500
Attempts:
2 left
💡 Hint

Only the row where name is 'Bob' is updated.

query_result
intermediate
2:00remaining
How many rows are updated?

Consider the table Products:

product_id | name       | stock
-----------+------------+-------
1          | Pen        | 100
2          | Pencil     | 200
3          | Notebook   | 150
4          | Eraser     | 50

After running this query, how many rows will have their stock increased?

UPDATE Products SET stock = stock + 10 WHERE stock < 150;
A2
B1
C3
D4
Attempts:
2 left
💡 Hint

Check which rows have stock less than 150.

📝 Syntax
advanced
2:00remaining
Which UPDATE query is syntactically correct?

Choose the correct SQL UPDATE statement that increases price by 20 for products with category 'Books'.

AUPDATE Products price = price + 20 WHERE category = 'Books';
BUPDATE Products SET price + 20 WHERE category = 'Books';
CUPDATE Products SET price = price + 20 WHERE category = 'Books';
DUPDATE Products SET price = price + 20 category = 'Books';
Attempts:
2 left
💡 Hint

Remember the syntax: UPDATE table SET column = value WHERE condition;

optimization
advanced
2:00remaining
Which query is more efficient to update salaries for employees in department 5?

Given a large Employees table with a department_id column, you want to increase salaries by 100 for all employees in department 5.

Which query is more efficient?

AUPDATE Employees SET salary = salary + 100;
BUPDATE Employees SET salary = salary + 100 WHERE department_id = 5;
CUPDATE Employees SET salary = salary + 100 WHERE department_id IN (SELECT department_id FROM Departments WHERE department_id = 5);
DUPDATE Employees SET salary = salary + 100 WHERE department_id > 0;
Attempts:
2 left
💡 Hint

Consider the condition that targets only department 5.

🔧 Debug
expert
2:00remaining
What error does this UPDATE query raise?

Consider this query:

UPDATE Employees SET salary = salary + 100 WHERE name = 'John' AND;

What error will the database return?

AConstraint violation error
BNo rows updated
CType error on salary column
DSyntax error near 'AND'
Attempts:
2 left
💡 Hint

Look carefully at the WHERE clause syntax.