Careeroza
SQL Syntax
SQL Statements
Most actions on a database are done with SQL statements. A statement is made of keywords (like SELECT and FROM), names (tables and columns) and values.
Example
The following statement selects all records in the Customers table:
SELECT * FROM Customers;Result: 8 rows
| CustomerID | CustomerName | ContactName | City | Country |
|---|---|---|---|---|
| 1 | Aarav Traders | Aarav Shah | Mumbai | India |
| 2 | Blue Harbor Foods | Emma Clark | London | UK |
| 3 | Cedar & Co | Liam Brown | Toronto | Canada |
… 5 more rows
Keep in Mind
- SQL keywords are NOT case sensitive:
selectis the same asSELECT. By convention keywords are written in UPPER CASE. - Table and column names may be case sensitive depending on the database and operating system (e.g. MySQL on Linux).
- Text values go inside single quotes:
'India'. Numbers are written without quotes. - A statement can span several lines; spaces and line breaks are ignored.
Semicolon after SQL Statements?
The semicolon is the standard way to separate statements, which lets you send more than one statement to the server in one call. Some tools do not require it for a single statement, but it is good practice to always end statements with ;.
Comments
Comments explain your SQL and are ignored when the statement runs.
Example
Single-line comments start with --; multi-line comments go between /* and */:
-- Customers from India
SELECT CustomerName, City
FROM Customers
/* Only rows where the country
is India are returned */
WHERE Country = 'India';Result: 2 rows
| CustomerName | City |
|---|---|
| Aarav Traders | Mumbai |
| Delta Mart | Bengaluru |
Some of The Most Important SQL Commands
| Command | What it does |
|---|---|
SELECT | extracts data from a database |
UPDATE | updates data in a database |
DELETE | deletes data from a database |
INSERT INTO | inserts new data into a database |
CREATE DATABASE | creates a new database |
CREATE TABLE | creates a new table |
ALTER TABLE | modifies a table |
DROP TABLE | deletes a table |
CREATE INDEX | creates an index (search key) |
DROP INDEX | deletes an index |