Skip to content
Advertisement

How to use SQL to find specific names or words in a database?

I am working with a database of data from Reddit. I want to find the name and description of all subreddits where the name starts with “covid” or “corona” and the description contains “covid” anywhere. I then want to output a table that has two columns: name and description.

So far, I have tried this SQL code:

select 
    subreddit AS name,
    subreddit AS description
    from
    subreddit
    where subr_description like "%covid%" "%corona%";
   

When I execute, I get an output of the table with the appropriate columns: name and description but they are empty, no data.

How can I have the table with the right columns populated with the right data please?

Advertisement

Answer

It looks like you need to adjust the column names that you’re selecting instead of subreddit AS x twice. I’m not sure if you even have a column named subreddit or not, but that also appears to be the table name, which is confusing.

Then you can adjust the logic in the WHERE to check your two conditions:

SELECT
    subreddit AS name,
    subr_description AS description
FROM
    subreddit
WHERE
    -- name starts with covid or corona ("text%")
    (subreddit LIKE "covid%" OR subreddit LIKE "corona%")
    -- description contains covid ("%text%")
    AND subr_description LIKE "%covid%"
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement