What is SQL Syntax?.

SQL is a declarative language that looks like natural English. At its core, an SQL statement begins with a verb describing the action, such as SELECT, INSERT, UPDATE, or DELETE, followed by the subject and predicate. This structure ensures that SQL commands read like everyday sentences.

Example:

SELECT
    product_name, price
FROM
    products
WHERE
    price < 50;

Here, we query product names and prices for items costing less than $50. The SELECT, FROM, and WHERE are SQL clauses, with some being mandatory (SELECT and FROM) and others optional (WHERE).

Understanding SQL Syntax

SQL's user-friendliness stems from its simplicity. Because SQL was designed specifically for the non-technical people. Unlike imperative languages like PHP or Java, SQL focuses on "what" you want instead of "how" you want it. This simplicity makes SQL accessible to all, from data analysts to developers and database administrators.

SQL Commands

SELECT
    emp_name, emp_id
FROM
    employees;

DELETE FROM employees
WHERE
     emp_id = 'E001';

SQL comprises a plethora of commands, each terminated with a semicolon (;). For instance, you can have two different SQL commands in a single script, separated by a semicolon:

The semicolon marks the end of a command, and each command consists of keywords, literals, identifiers, or expressions. Tokens are separated by spaces, tabs, or newlines.

Literals

SQL offers three types of literals:

  • String
  • Numeric
  • Binary

String literals are alphanumeric characters enclosed in single quotes, e.g., 'Mark','100' ,'1996-08-15'. Numeric literals can be integers, decimals, or scientific notations, such as 200 or 6.0221415E23. SQL represents binary values as x'0000', with each digit being a hexadecimal value.

Keywords

SQL boasts numerous keywords with special meanings, like SELECT, INSERT, UPDATE, DELETE, and DROP. These keywords are reserved, meaning they can't be used as table, column, index, or view names.

  1. ADD
  2. ADD CONSTRAINT
  3. ALL
  4. ALTER
  5. ALTER COLUMN
  6. ALTER TABLE
  7. AND
  8. ANY
  9. AS
  10. ASC
  11. BACKUP DATABASE
  12. BETWEEN
  13. CASE
  14. CHECK
  15. COLUMN
  16. CONSTRAINT
  17. CREATE
  18. CREATE DATABASE
  19. CREATE INDEX
  20. CREATE OR REPLACE VIEW
  21. CREATE TABLE
  22. CREATE PROCEDURE
  23. CREATE UNIQUE INDEX
  24. CREATE VIEW
  25. DATABASE
  26. DEFAULT
  27. DELETE
  28. DESC
  29. DISTINCT
  30. DROP
  31. DROP CONSTRAINT
  32. DROP DATABASE
  33. DROP DEFAULT
  34. DROP INDEX
  35. DROP TABLE
  36. DROP VIEW
  37. EXEC
  38. EXISTS
  39. FOREIGN KEY
  40. FROM
  41. FULL OUTER JOIN
  42. GROUP BY
  43. HAVING
  44. IN
  45. INDEX
  46. INNER JOIN
  47. INSERT INTO
  48. INSERT INTO
  49. IS NULL
  50. IS NOT NULL
  51. JOIN
  52. LEFT JOIN
  53. LIKE
  54. LIMIT
  55. NOT
  56. NOT NULL
  57. OR
  58. ORDER BY
  59. OUTER JOIN
  60. PRIMARY KEY
  61. PROCEDURE
  62. RIGHT JOIN
  63. ROWNUM
  64. SELECT
  65. SELECT DISTINCT
  66. SELECT INTO
  67. SELECT TOP
  68. SET
  69. TABLE
  70. TOP
  71. TRUNCATE TABLE
  72. UNION
  73. UNION ALL
  74. UNIQUE
  75. UPDATE
  76. VALUES
  77. VIEW
  78. WHERE

Comments

To document SQL statements, use comments. Database engines ignore characters in comments. Single-line comments start with --, while multiline comments use /* */.

SELECT
    student_id, name
FROM
    student
WHERE
    marks < 30; -- Students with low marks