SQL Syntax

Anant Dhakad
Last Updated: May 13, 2022

Introduction

If you wish to do some operations on the database's data, you must write the query in SQL's standard syntax.

The SQL's syntax is a distinct set of rules and standards that are not case-sensitive. The ISO and ANSI standards define and maintain its syntax.

This blog will provide you with a quick introduction to SQL by listing all the essential SQL syntaxes.

SQL syntax

The following are some of the most significant points to remember about SQL syntax:

  • Although we can write SQL keywords in both uppercase and lowercase, writing them in uppercase improves the readability of the SQL query.
  • Text lines affect SQL statements and syntax. We can place a single SQL statement on one or more text lines.
  • SQL statements can be used to conduct most of the actions in a database.
  • SQL syntax is based on tuple relational calculus and relational algebra.

SQL statements

SQL statements instruct the database about the operation you want to do on the structured data and the information you want to get from it.

SQL statements are straightforward to use and comprehend. They're similar to plain English but with a specific syntax.

Example

SELECT col1, col2 FROM tableName;


Each SQL statement starts with a SQL term and concludes with a semicolon( ";" ). In SQL, the semicolon is used to separate numerous SQL statements that will be executed in the same call. 

Some of The Most Important SQL Commands

SELECT Statement

This SQL statement retrieves information from a SQL database and displays it as output to the database user.

SELECT col1, col2....colN
FROM   tableName;

WHERE Clause

The records are filtered using this SQL query. Only queries that satisfy the following CONDITIONs are returned.

SELECT col1, col2....colN
FROM   tableName
WHERE  CONDITION;

IN Clause

The "IN" operator reduces the need for several  "OR" conditions in a SQL query.

SELECT col1, col2....colN
FROM   tableName
WHERE  columnName IN (val1, val2,...valN);

ORDER BY Clause

When we wish to arrange records based on fields stored in SQL database tables, we use the "ORDER BY" clause in SQL.

SELECT      col1, col2....colN
FROM        tableName
WHERE       CONDITION
ORDER BY    columnName {ASC|DESC};

GROUP BY Clause

The "Group By" statement in SQL is used to group similar records.

SELECT      SUM(columnName)
FROM        tableName
WHERE       CONDITION
GROUP BY    columnName;

HAVING Clause

The HAVING clause in the SELECT statement places the condition in the groups indicated by the GROUP BY clause.

SELECT      SUM(columnName)
FROM        tableName
WHERE       CONDITION
GROUP BY    columnName
HAVING      (CONDITION);

CREATE DATABASE Statement

It creates a database named after the name supplied in the Create Database statement.

CREATE DATABASE databaseName;

CREATE TABLE Statement

To build a table in a database, use the “CREATE TABLE” statement.

CREATE TABLE tableName(
    col1 datatype,
    col2 datatype,
    col3 datatype,
    .....
    colN datatype,
    PRIMARY KEY( one or more columns )
);

ALTER TABLE Statement

ALTER TABLE tableName {ADD|DROP|MODIFY} columnName {datatype};

DROP TABLE Statement

A DROP TABLE statement deletes a table's definition and all of its contents.

DROP TABLE tableName;

INSERT INTO Statement

This SQL statement adds new data or records to an existing database table. This statement can quickly insert single and numerous records in a single query line.

INSERT INTO tableName( col1, col2....colN)
VALUES ( val1, val2....valN);

UPDATE Statement

The current records in the SQL database are changed or modified by this SQL statement.

UPDATE tableName
SET col1 = val1, col2 = val2....colN=valN
[ WHERE  CONDITION ];

DELETE Statement

This statement deletes existing records from the table.

DELETE FROM tableName
[ WHERE  CONDITION ];

FAQs

  1. List some of the aggregate functions used in SQL?
    Sum, count, min, max are some of the aggregate functions used in SQL.
     
  2. List some of the data types in SQL? 
    Char(size), Varchar(length), Text, Integer are some of the data types in SQL.
     
  3. What are the different types of case manipulation functions available in SQL?
    Three different types of manipulation functions in SQL are:
    - LOWER: returns the string in lowercase.
    - UPPER: returns the string in uppercase.
    - INITCAP: returns the string with the first letter as uppercase and the rest in lowercase letters.
     
  4. What is a Primary Key?
    The PRIMARY KEY constraint uniquely identifies each row in a table. It contains an implied NOT NULL condition and must include UNIQUE data.

Key Takeaways

Cheers if you reached here!! In this blog, we learned about some basic syntax of SQL.

Don't stop here, Ninja; check out the Top 100 SQL Problems to get hands-on experience with frequently asked interview questions and land your dream job.

Yet learning never stops, and there is a lot more to learn. Happy Learning!!

Was this article helpful ?
0 upvotes

Comments

No comments yet

Be the first to share what you think