-1

I'm watching a video on SQL and the guy is making a distinction between ' and ". In Python and R, I tend to use ' and " interchangeably while technically there is a minor difference or in certain situations one needs to be used. However for the most part in Python/R it doesn't matter.

Is this the same in SQL and the guy is just over analyzing or is there actually a big difference between ' and "? Unfortunately, I don't actually work with SQL so I can't really learn from experience - just have to watch vids.

Thanks

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • 1
    That depends entirely on which server you're using, and in some cases possibly even the client library. – Jonathan Hall Sep 30 '20 at 08:15
  • What SQL server you are referring to – Shubham Srivastava Sep 30 '20 at 08:15
  • "I don't actually work with SQL so I can't really learn from experience - just have to watch vids" -- There's nothing stopping you from installing an SQL server locally and _gaining_ experience. – Jonathan Hall Sep 30 '20 at 08:15
  • But I don't have any databases. I'm not sure. The guy uses SQLLite. – we_are_all_in_this_together Sep 30 '20 at 08:16
  • Does this answer your question? [What is the difference between single and double quotes in SQL?](https://stackoverflow.com/questions/1992314/what-is-the-difference-between-single-and-double-quotes-in-sql) – Ivar Sep 30 '20 at 08:17
  • 1
    SQLLite is free, and easy to install. – Jonathan Hall Sep 30 '20 at 08:17
  • @Flimzy So what's the point of job apps saying "know SQL" when syntax differs per company? – we_are_all_in_this_together Sep 30 '20 at 08:17
  • 1
    The differences between SQL servers are minimal, and don't change the fundamentals. When someone talks about "Do you know SQL?" they're talking about the fundamentals. It's like the difference between American, British, and Australian English. You may not know the proper slang term for something in Australia, but you sure as heck speak English. – Jonathan Hall Sep 30 '20 at 08:18
  • @Ivar I know what the difference is, but wondering if it actually makes a difference in practice. I tried some stuff on W3.com and doesn't seem like ' or " makes any difference. Seems like I can even surround numbers with " and I still get the result I want. I just want to know if in reality it makes a big difference or not just like in practice it doesn't make much difference in R/Python - even though there is a "best practice" if you are a hardcore programmer – we_are_all_in_this_together Sep 30 '20 at 08:21
  • 1
    @we_are It depends on the DBMS you are using. Some allow both, some allow only one of the two and will throw an error when you use the other one. That being said I'm not too sure what you mean by W3.com, but don't confuse W3Schools with W3C. They are in no way related and W3Schools often is (or at least was) [considered a bad resource](https://meta.stackoverflow.com/questions/280478/why-not-w3schools-com). – Ivar Sep 30 '20 at 08:28
  • @Ivar Thanks, the vid I"m watching is from LInkedIn Learning but since I don't have SQLLite, I use W3Schools.com to practice code. They have some databases there https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all – we_are_all_in_this_together Sep 30 '20 at 08:36

1 Answers1

2

Yes, ' and " are very different in SQL.

  • ' is used to indicate strings, such as 'Hello world!'; it is always required.

  • " is used to indicate identifiers, such as SELECT "name", "age" FROM "people"; it's optional when the name can't be confused with anything, but mandatory if you want unusual names (spaces, upper-case letters) or if you want a name that's reserved (such as "from"); for example SELECT "from", "to" FROM "time slots".

    It's generally best to avoid names that have to be quoted, but the option is there if you need it. Many libraries that wrap SQL will habitually quote everything.

Jiří Baum
  • 6,697
  • 2
  • 17
  • 17
  • 3
    One should maybe add that different databases interpret the quotes differently: some allow single quotes for identifiers, others allow double quotes for strings - which makes things a bit messy. But your answer is correct in general, and matches standard SQL rules. – GMB Sep 30 '20 at 08:24