SQL Tutorial menu

SQL Syntax

SQL has a few simple writing rules. Learn them once and every later lesson will be easier to read.

SQL Statements

A command you send to the database is called a statement. Most actions in a database are done with statements.

Example

sql
SELECT * FROM students;

This statement reads every column of every row from the students table.

A statement is made of:

  • Keywords, words with a special meaning such as SELECT, FROM and WHERE
  • Names of tables and columns, such as students and city
  • Values, such as 'Karachi' or 20

Keywords Are Not Case Sensitive

SELECT, select and Select all mean the same thing. These two statements give the same result:

Example

sql
SELECT name FROM students;
select name from students;

Most developers write keywords in CAPITAL letters and names in lowercase. It makes a query much easier to read, so this tutorial does the same.

Semicolon at the End

The semicolon ; marks the end of a statement. It lets you send several statements one after another:

Example

sql
SELECT name FROM students;
SELECT title FROM courses;

Some tools run a single statement without a semicolon, but always adding it is a good habit.

Spaces and New Lines

SQL does not care about extra spaces or line breaks. A long query is easier to read when each part is on its own line:

Example

sql
SELECT name, city, marks
FROM students
WHERE marks > 80;
namecitymarks
Ayesha SiddiquiLahore85
Fatima NoorIslamabad92
Sana IqbalLahore88

Text Values and Numbers

Text values go inside single quotes. Numbers are written without quotes.

Example

sql
SELECT name FROM students WHERE city = 'Lahore';
SELECT name FROM students WHERE age = 22;

If a text value itself has a single quote in it, write two single quotes: 'Ali''s laptop'.

Comments

Comments are notes for people. The database ignores them.

Example

sql
-- This is a one-line comment
SELECT name FROM students; -- a comment can also end a line

/* This is a comment
   over several lines */
SELECT title FROM courses;

The Most Important SQL Commands

CommandWhat it does
SELECTReads data from a table
INSERT INTOAdds new rows
UPDATEChanges existing rows
DELETERemoves rows
CREATE DATABASEMakes a new database
CREATE TABLEMakes a new table
ALTER TABLEChanges the columns of a table
DROP TABLEDeletes a whole table

You will learn each of these in the following lessons.

Exercise

Exercise

How should the text value Karachi be written in a SQL statement?

Learn SQL with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp