Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

Blake Wood
Blake Wood
1,396 Points

Deleting names

Question: Now we're using a database from a smartphone. It has a phone_book table. It has the columns id, first_name, last_name and phone. Delete all contacts with the first name of Jonathan and last name of Luna.

My code (that is wrong) is:

DELETE FROM phone_book WHERE first_name = ("Jonathan" AND "Luna");

Please advise where i am going wrong.

9 Answers

HIDAYATULLAH ARGHANDABI
HIDAYATULLAH ARGHANDABI
21,058 Points
DELETE FROM phone_book WHERE first_name = "Jonathan" AND last_name = "Luna";
Mike Costa
PLUS
Mike Costa
Courses Plus Student 26,362 Points

If you're looking to delete a single contact with first_name of "Jonathan" and last_name of "Luna" then your query should be:

DELETE FROM phone_book WHERE first_name = "Jonathan" AND last_name = "Luna";

If you're trying to delete ALL Jonathan's and ALL Luna's from the phone book, your query should be:

DELETE FROM phone_book WHERE first_name = "Jonathan" OR last_name = "Luna";

Following all the other queries wouldn't achieve what you're trying to accomplish.

Thomas is correct that

DELETE FROM phone_book WHERE first_name in ("Jonathan", "Luna");

is the same as

DELETE FROM phone_book WHERE first_name = "Jonathan" OR  first_name = "Luna";

But what that query will do is delete every contact with the first name of Jonathan, and the first name of Luna.

Hope this clears things up a bit.

It still doesn't work!!!

PLEASE HELP!!

PLEASE HELP!!!

Thomas Nilsen
Thomas Nilsen
14,957 Points

try

DELETE FROM phone_book WHERE first_name in ("Jonathan", "Luna");

this is the answer...

DELETE FROM phone_book WHERE first_name = "Jonathan" AND last_name = "Luna";

DELETE FROM phone_book WHERE first_name LIKE "Jonathan%" AND last_name LIKE "%Luna";

It says delete all contacts with first name Jonathan and last name Luna, i believe we are suppose to delete all instances of the names so below is the correct answer. DELETE FROM phone_book WHERE first_name LIKE "Jonathan%" AND last_name LIKE "%Luna";

You only giving a value for the first name. here is a template for your solution, remember you have two conditions

DELETE FROM <table> WHERE <condition> AND <condition>

The table is phone_book while the conditions are the first_name = "Jonathan" and the last_name = "Luna"

DELETE FROM phone_book WHERE first_name = "Jonathan" AND last_name = "Luna"
Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

I would separate your conditons & repeat the AND:

DELETE FROM phone_book WHERE first_name = "Jonathan" AND first_name = "Luna";

If that doesnt work try the EQUALS keyword.

Thomas Nilsen
Thomas Nilsen
14,957 Points

I don't think that will work. First_name cannot be Jonathan AND Luna at the same time. Unless I misunderstood the question

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

Then cant you use the OR instead of AND? You are right abou the AND....

Thomas Nilsen
Thomas Nilsen
14,957 Points
DELETE FROM phone_book WHERE first_name in ("Jonathan", "Luna");

is the same as

DELETE FROM phone_book WHERE first_name = "Jonathan" OR  first_name = "Luna";