Tanner Temoshenko
Tanner Temoshenko

Reputation: 119

How to update multiple ids at once

I just began learning how to code w/ SQL. I am following a tutorial on Codeacedemy.com

Here is something I wrote for the fun of it, a simple date base:

CREATE TABLE employees (id INTEGER, name TEXT, year INTEGER);
INSERT INTO employees (id, name, year) VALUES (1, 'Dave', 2010);
INSERT INTO employees (id, name, year) VALUES (2, 'Karen', 2001);
INSERT INTO employees (id, name, year) VALUES (3, 'Joe', 2009);
INSERT INTO employees (id, name, year) VALUES (4, 'Larry', 2013);
INSERT INTO employees (id, name, year) VALUES (5, 'Tammy', 2015);
INSERT INTO employees (id, name, year) VALUES (6, 'Samantha', 2005);
INSERT INTO employees (id, name, year) VALUES (7, 'Karen', 2010);
INSERT INTO employees (id, name, year) VALUES (8, 'Rick', 2011);

ALTER TABLE employees ADD COLUMN gender TEXT;

UPDATE employees
set gender = 'Male'
where id = 1;

SELECT * FROM employees;

Is there a way that I can update multiple rows at once using their id? For example, I can use id 1, 3, 5, 8 and they'll all be updated to 'male'.

Thanks!

Upvotes: 10

Views: 45284

Answers (2)

Mansoor
Mansoor

Reputation: 4192

This for using where clause :

 UPDATE employees SET gender = 'Male' WHERE id IN (1,2,3)

If you want update all rows in table then:

UPDATE employees SET gender = 'Male'  

Upvotes: 24

Pritam Banerjee
Pritam Banerjee

Reputation: 18923

You can use the same thing, but for the ID's you can use ID's in

Like this:

 ....
 where id in (1,2,3);

You can also write a nested query inside IN.

For more details on how to write IN you can refer here.

Upvotes: 6

Related Questions