What does the SQL Standard say about usage of backtick(`)?

The SQL standard (current version is ISO/IEC 9075:2011, in multiple parts) says nothing about the ‘back-tick’ or ‘back-quote’ symbol (Unicode U+0060 or GRAVE ACCENT); it doesn’t recognize it as a character with special meaning that can appear in SQL. The Standard SQL mechanism for quoting identifiers is with delimited identifiers enclosed in double quotes: SELECT … Read more

Oracle SQL, concatenate multiple columns + add text

You have two options for concatenating strings in Oracle: CONCAT Using || CONCAT example: CONCAT( CONCAT( CONCAT( CONCAT( CONCAT(‘I like ‘, t.type_desc_column), ‘ cake with ‘), t.icing_desc_column), ‘ and a ‘), t.fruit_desc_column) Using || example: ‘I like ‘ || t.type_desc_column || ‘ cake with ‘ || t.icing_desc_column || ‘ and a ‘ || t.fruit_desc_column

How to include “zero” / “0” results in COUNT aggregate?

You want an outer join for this (and you need to use person as the “driving” table) SELECT person.person_id, COUNT(appointment.person_id) AS “number_of_appointments” FROM person LEFT JOIN appointment ON person.person_id = appointment.person_id GROUP BY person.person_id; The reason why this is working, is that the outer (left) join will return NULL for those persons that do not … Read more

Date Difference between consecutive rows

SELECT T1.ID, T1.AccountNumber, T1.Date, MIN(T2.Date) AS Date2, DATEDIFF(“D”, T1.Date, MIN(T2.Date)) AS DaysDiff FROM YourTable T1 LEFT JOIN YourTable T2 ON T1.AccountNumber = T2.Accountnumber AND T2.Date > T1.Date GROUP BY T1.ID, T1.AccountNumber, T1.Date; or SELECT ID, AccountNumber, Date, NextDate, DATEDIFF(“D”, Date, NextDate) FROM ( SELECT ID, AccountNumber, Date, ( SELECT MIN(Date) FROM YourTable T2 WHERE T2.Accountnumber … Read more