FAQ: Manipulation - Delete

This community-built FAQ covers the “Delete” exercise from the lesson “Manipulation”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development
Data Science

Learn SQL

FAQs on the exercise Delete

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

In SQL we use DELETE to remove rows, what if I want to remove columns? Do I use ALTER?

there is a fundamental difference between removing rows and removing columns.

for example, on this website we have different users (lets say in a user table), then the columns could be: email, username, password and so forth.

when you registered your account on this website, a row got added to the user table. If you now wanted to remove your account, your row would get deleted

so rows are very often generated by people using the application while the columns are set up by the programmers. So modifying rows (users, posts, likes) happens a lot more often

ALTER TABLE table_name DROP COLUMN column_name; would work, but removing columns is far less common and requires care and planning. You need to make sure that the column isn’t used anywhere in your application anymore.

1 Like

When to use = or IS?
When I used
WHERE twitter_handle = NULL

it didn’t delete everything, but when I used
WHERE twitter_handle IS NULL
it deleted everything.

When I tried
WHERE id = 4, it worked well.

1 Like

NULL is a special value, which requires IS for comparison.

1 Like

Why id 4 is the only one left in the table. I know the other 3 were deleted but why. Please help me understand this.

because they met the where condition? Any rows meeting the condition are removed.

If we look at the data before the removal:

image

we can see only taylor swift has a twitter handle.

why are the other 3 are null because didn’t we just enter all the similar text for all 4 of the id

no, in the previous you only updated taylor swift her twitter handler:

UPDATE celebs 
SET twitter_handle = '@taylorswift13' 
WHERE id = 4; 

the where ensures that (where id equals 4, taylor has id 4)

But I updated all 4 with similar TEXT.

all 4 what? You only updated one column (twitter_handle) of a single row.

all 4 id. I updated ‘@justinbie15’ for 1 and ‘@beyknowles16’ for 2 and another one for 3

I did run it and got a table with all 4 TEXT in the table

But the exercise only asks to update a single row, so that is then the starting point of the next exercise. Additional queries executed are not carried over to the next exercise.

In transact SQL you cannot use equal operator to compare a NULL value. Think about it in the sense of NULL is not a known value. Basically the system does not know or cannot determine a specific value for NULL. In mathematics, equals operation always requires you to know the specific value to which you are applying the operation. So = NULL will work but it returns nothing. This is because it didn’t know what to do with that operation simply, but it is a valid operation.

Where are IS NULL and IS NOT NULL are used to simply determine “do we know?” or “don’t we know?”.

To perform operations on NULL (non deterministic values) you cannot use standard mathematical operations and have to rely on the IS operator.

Hello,
When I use the following clause, the query results are exactly what they’re supposed to be - all the rows containing NULL as a twitter_handle are gone.

DELETE FROM celebs
 WHERE twitter_handle IS NULL;
 
 SELECT * FROM celebs; 

But, afterwards, if I run JUST

SELECT * FROM celebs;

Than I see the original results again, without any rows deleted at all.

Question is, how do I delete rows permanently?
If that’s even possible…

UPDATE:
After moving on to the next Exercise, and using this clause, I see that the table in fact did update and deleted every row with NULL.

So that was the issue? I needed to refresh the page or something?

Codecademy is a teaching environment, the data persistent might not be perfect. Done deliberately, because its about learning on codecademy

Why does my Select * statement need to be after my delete statement?

Logically to me it would make sense to state,

Select *
THEN use my delete statement, rather than delete these columns from data I haven’t selected yet

delete deletes a row, not specific columns.

Thanks for the clarity. I’m still not understanding why my select * must be after my delete statement