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).
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.
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.
SQL offers three types of literals:
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.
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.
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