SQL Syntax (2024)

SQL Statements

Most of the actions you need to perform on a database are done with SQL statements.

SQL statements consists of keywords that are easy to understand.

The following SQL statement returns all records from a table named "Customers":

Example

Select all records from the Customers table:

SELECT * FROM Customers;

Try it Yourself »

In this tutorial we will teach you all about the different SQL statements.

Database Tables

A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders"), and contain records (rows) with data.

In this tutorial we will use the well-known Northwind sample database (included in MS Access and MS SQL Server).

Below is a selection from the Customers table used in the examples:

CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden

The table above contains five records (one for each customer) and seven columns (CustomerID, CustomerName, ContactName, Address, City, PostalCode, and Country).

Keep in Mind That...

  • SQL keywords are NOT case sensitive: select is the same as SELECT

In this tutorial we will write all SQL keywords in upper-case.

Semicolon after SQL Statements?

Some database systems require a semicolon at the end of each SQL statement.

Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server.

In this tutorial, we will use semicolon at the end of each SQL statement.

Some of The Most Important SQL Commands

  • 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
  • ALTER DATABASE - modifies a 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

W3schools Pathfinder

Track your progress - it's free!

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.

SQL Syntax (2024)

FAQs

What is the basic syntax of SQL? ›

SQL Quick Reference from W3Schools
SQL StatementSyntax
ORDER BYSELECT column_name(s) FROM table_name ORDER BY column_name [ASC|DESC]
SELECTSELECT column_name(s) FROM table_name
SELECT *SELECT * FROM table_name
SELECT DISTINCTSELECT DISTINCT column_name(s) FROM table_name
29 more rows

What is in syntax in SQL? ›

The IN command allows you to specify multiple values in a WHERE clause. The IN operator is a shorthand for multiple OR conditions.

Is SQL syntax easy? ›

My view is that it is fairly easy-ish for a beginner to learn the immediate basics, but SQL also has a number of extremely non-trivial considerations (trinary logic as well as the fact that the same syntax will result in potentially different behavior depending on the database system and SQL dialect) that make even ...

How to write SQL syntax? ›

How to Create a SQL Statement
  1. Start your query with the select statement. select [all | distinct] ...
  2. Add field names you want to display. field1 [,field2, 3, 4, etc.] ...
  3. Add your statement clause(s) or selection criteria. Required: ...
  4. Review your select statement. Here's a sample statement:
Oct 13, 2022

Is SQL hard to learn? ›

Learning SQL is generally considered easier than many other programming languages, and mastery of SQL can facilitate learning other programming languages such as Python or JavaScript. Knowledge of SQL can open many professional doors across various industries including finance, social media, and music.

How to learn SQL for beginners? ›

Learning & practice tools
  1. Articles. Learn about technical concepts.
  2. Cheatsheets. Review concepts from your courses.
  3. Code challenges. Test your knowledge and prep for interviews.
  4. Projects. Practice and build your portfolio.
  5. Videos. Watch tutorials, project walkthroughs, and more.
  6. Workspaces.

How to learn basic SQL commands? ›

Some of The Most Important SQL Commands
  1. SELECT - extracts data from a database.
  2. UPDATE - updates data in a database.
  3. DELETE - deletes data from a database.
  4. INSERT INTO - inserts new data into a database.
  5. CREATE DATABASE - creates a new database.
  6. ALTER DATABASE - modifies a database.
  7. CREATE TABLE - creates a new table.

What is the basic knowledge of SQL? ›

Structured query language (SQL) is a programming language for storing and processing information in a relational database. A relational database stores information in tabular form, with rows and columns representing different data attributes and the various relationships between the data values.

What is SQL syntax with an example? ›

An SQL SELECT statement retrieves records from a database table according to clauses (for example, FROM and WHERE ) that specify criteria. The syntax is: SELECT column1, column2 FROM table1, table2 WHERE column2='value';

What is the most common syntax in SQL? ›

SELECT is probably the most commonly-used SQL statement. You'll use it pretty much every time you query data with SQL. It allows you to define what data you want your query to return. For example, in the code below, we're selecting a column called name from a table called customers .

What is a query syntax? ›

The term syntax refers to strict structural patterns used when creating a query. As soon as you enter the search criteria using the correct syntax, the query should execute, and the requested records retrieved from the target database.

Is SQL hard than Python? ›

Unlike other programming languages, such as Python, SQL provides an easy-to-learn syntax and a simple interface for creating SQL statements.

Is SQL tough or Python? ›

Compared to Python, SQL is a much simpler language. It's also exclusively used for data. That means it's easier to learn, and it provides the quickest, most efficient means of performing simple data analyses.

Can I teach myself SQL? ›

Even if you lack experience with web or software development, you can begin learning SQL on your own and start working toward a mastery of the language.

What is SQL and its syntax? ›

Structured Query Language (SQL) is defined as a standard programming language utilized to extract, organize, manage, and manipulate data stored in relational databases. This article explains the fundamentals of the SQL programming language, its elements, examples, and uses in 2022.

What is basic syntax? ›

In linguistics, syntax refers to the rules that govern the formation of grammatically correct sentences. Similarly, in computer programming, syntax refers to the rules that govern the structure of the symbols, words, and punctuation found in programming languages.

Top Articles
Latest Posts
Article information

Author: Fredrick Kertzmann

Last Updated:

Views: 6595

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Fredrick Kertzmann

Birthday: 2000-04-29

Address: Apt. 203 613 Huels Gateway, Ralphtown, LA 40204

Phone: +2135150832870

Job: Regional Design Producer

Hobby: Nordic skating, Lacemaking, Mountain biking, Rowing, Gardening, Water sports, role-playing games

Introduction: My name is Fredrick Kertzmann, I am a gleaming, encouraging, inexpensive, thankful, tender, quaint, precious person who loves writing and wants to share my knowledge and understanding with you.