How to Use Single Quotes in SQL

Many SQL beginners wonder how to use single quotes in SQL. It is very simple to do, but there are some things to keep in mind.

close up shot of a person using a laptop

Single quotes are a special character in Structured Query Language (SQL). They are used in queries to denote a “string”, which is simply a series of characters. You may see queries that look similar to this:

SELECT *
FROM people
WHERE first_name = 'Alex'

In this case, the single quotes are used to denote the string “Alex”. We assume that our first_name column is one of the character data types here. As a result, if we run the query without single quotes, we will get an error:

SELECT *
FROM people
WHERE first_name = Alex

Without the single quotes, SQL will think that Alex is a column name in the table people. The quotes are what indicate to SQL that “Alex” is a value, not a column.

Single Quotes versus Double Quotes

In SQL, single quotes and double quotes are not interchangeable the way that they are in other programming languages. Double quotes don’t carry any specific significance. They cannot be used to indicate a value. The following query will result in an error:

SELECT *
FROM people
WHERE first_name = "Alex"

If we were to put single quotes around this value, then SQL would look for any records where the first name is “Alex” including the double quotes:

SELECT *
FROM people
WHERE first_name = '"Alex"'

Escaping Strings in SQL

What happens if you need to search for values that contain single quotes? What if you need to insert a value into your database that has single quotes in it? In this case, you would use a method that we call “escaping”. By doing this, you’re indicating to the database that it should treat the quote character as an actual quote character, not as a special identifier.

To escape a single quote, we simply use two single quotes consecutively. Let’s look at an INSERT query to see how we would get the data into the database.

INSERT INTO hospitals (hospital_name)
VALUES ('Children''s Hospital of Philadelphia')

Take careful note that in the word “Children’s”, we have two single quotes. This is what it looks like when the single quote is properly escaped. If we want to run a query to find the record in the table, we would use the same value in our WHERE clause, like this:

SELECT *
FROM hospitals
WHERE hospital_name = 'Children''s Hospital of Philadelphia'

You should always be careful to ensure that you’re treating single quotes correctly in your queries. Not doing so will result in errors being returned when the database tries to run the query.

The good news here is that all databases that use the SQL ANSI standard – which is virtually every relational database – treat single quotes in the same way. So you don’t have to remember anything different based on whether you’re using Microsoft SQL Server, MySQL, PostgreSQL, or some other database.

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?