Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele UPDATE and DELETE | DDL and DML in SQL
Intermediate SQL
course content

Kurssisisältö

Intermediate SQL

Intermediate SQL

1. Grouping
2. Nested Subqueries
3. Joining Tables
4. DDL and DML in SQL

book
UPDATE and DELETE

It's time to get back to writing queries and consider two more statements.

You already know how to clear a table, add a column, insert data, etc. But for proper interaction with the database, we need to understand how to update and delete individual rows.

For this, there are two statements and types of queries: the UPDATE and the DELETE queries.

Let's look at an example with the medications table:

Let's imagine we need to update the price for a specific type of medication. For example, currently, there's a 50% discount on ibuprofen, and we need to change the price for this product.

Our UPDATE query will look like this:

1234567
UPDATE medications SET price = 4 WHERE id = 2; SELECT * FROM medications ORDER BY id;
copy

Here, we updated the medications table so that the price for the product with id 2 (ibuprofen) will be set to 4. After that, we selected all the columns from the table to ensure the price column was successfully updated. You can substitute any value and see how the update operation works in SQL:

sql

The syntax for deletion will look like this:

sql

But I'll remind you that deleting rows should be done with caution, as you won't be able to simply recover them.

Note

If you don't include a WHERE condition, the data will be updated or deleted for all rows.

Tehtävä

Swipe to start coding

In the pharmaceutical market, there's inflation!

The supplier company informed us that we must urgently establish a minimum price of 10 units for the product. So your task will be to update the table so that the price, which was previously less than 10, becomes 10. You can do this using the UPDATE statement.

Brief Instructions

  • Use the UPDATE statement to modify the medications table.
  • Use SET to assign a new value of 10 to the price column.
  • Use the WHERE clause with the condition price < 10 so that the update only applies to rows meeting this requirement.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 4
toggle bottom row

book
UPDATE and DELETE

It's time to get back to writing queries and consider two more statements.

You already know how to clear a table, add a column, insert data, etc. But for proper interaction with the database, we need to understand how to update and delete individual rows.

For this, there are two statements and types of queries: the UPDATE and the DELETE queries.

Let's look at an example with the medications table:

Let's imagine we need to update the price for a specific type of medication. For example, currently, there's a 50% discount on ibuprofen, and we need to change the price for this product.

Our UPDATE query will look like this:

1234567
UPDATE medications SET price = 4 WHERE id = 2; SELECT * FROM medications ORDER BY id;
copy

Here, we updated the medications table so that the price for the product with id 2 (ibuprofen) will be set to 4. After that, we selected all the columns from the table to ensure the price column was successfully updated. You can substitute any value and see how the update operation works in SQL:

sql

The syntax for deletion will look like this:

sql

But I'll remind you that deleting rows should be done with caution, as you won't be able to simply recover them.

Note

If you don't include a WHERE condition, the data will be updated or deleted for all rows.

Tehtävä

Swipe to start coding

In the pharmaceutical market, there's inflation!

The supplier company informed us that we must urgently establish a minimum price of 10 units for the product. So your task will be to update the table so that the price, which was previously less than 10, becomes 10. You can do this using the UPDATE statement.

Brief Instructions

  • Use the UPDATE statement to modify the medications table.
  • Use SET to assign a new value of 10 to the price column.
  • Use the WHERE clause with the condition price < 10 so that the update only applies to rows meeting this requirement.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 4
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt