SQL Syntax Commands

SQL Syntax: This is an extended post to the SQL Online Tutorial series Blog posts. In this post, we will see various SQL Syntax. Because if your basics on SQL Syntax are clear, it helps you understand the SQL statements easily.

SQL (Structured Query Language) is a programming language mainly used to manage and manipulate relational databases. So when you want to perform some operation on the data available on the database, you have to write a query for that, and that SQL query should follow the predefined SQL syntax.

If the user is not writing the SQL syntax correctly, the Query will not be executed, and the SQL server will give you an error message. So to get the requested records retrieved from the target database, you have to write the query in the correct SQL syntax.

SQL Syntax Key Points to Remember

Also, while writing the SQL queries, you have to keep in mind the below important points:

  • In a SQL statement, the first word will always be a SQL Keyword. And SQL keywords are the words that have a specific meaning in the SQL language, such as SELECT, FROM, WHERE, etc. These keywords can be written in both uppercase and lowercase letters, but it is common practice to write them in uppercase to improve the readability of the SQL query. This is because uppercase letters stand out more than lowercase letters and make it easier to identify the keywords in the query.
  • SQL syntax must end with a Semicolon (;).
  • SQL syntax is based on mathematical concepts called relational algebra and tuple relational calculus. These concepts define the relationships between data in a database and the operations that can be performed on that data. This means the SQL language is designed to work with data in a structured, relational way, allowing users to manipulate and query the data logically and mathematically.
  • When creating table and column names, be sure to use lowercase letters. If you need to use multiple words, you can separate them with an underscore (_) or camelCase.
  • If you are working with any String, you need to use single quotes (‘ ‘);
  • And during working with numeric values, they should not be enclosed in quotes.
  • While working with dates, the date format should be in “YYYY-MM-DD” format, and date values should be enclosed by single quotes (‘ ‘).

Remember that the SQL syntax may vary slightly depending on your database management system.

SQL Syntax Commands

Below we have listed some more SQL syntaxes commonly used in managing and manipulating relational databases. But we will discuss those SQL commands in detail and learn how to use those efficiently.

Select Statement

The SELECT statement is used to retrieve data from a database. It specifies which columns to retrieve data from and from which table. The basic syntax of a SELECT statement is:

SELECT "column_name" FROM "table_name";

DISTINCT Keyword

The DISTINCT keyword retrieves unique values from a column in a table. The basic syntax of a SELECT statement with the DISTINCT keyword is:

SELECT DISTINCT "column_name"
FROM "table_name";

WHERE Clause

The WHERE clause is used to filter records based on certain conditions. It is used to specify the conditions that must be met for the records to be returned. The basic syntax of a WHERE clause is:

SELECT "column_name"
FROM "table_name"
WHERE "condition"

And/Or Operators

In SQL, the “and” and “or” operators are used to combine multiple conditions in a WHERE clause. The syntax for the “and” and “or” operators in SQL is as follows:

SELECT "column_name"
FROM "table_name"
WHERE "simple condition"
{[AND|OR] "simple condition"};

IN Operator

The IN operator is used to specify multiple values in a WHERE clause. The basic syntax of a SELECT statement with the IN operator is:

SELECT "column_name"
FROM "table_name"
WHERE "column_name" IN ('value1', 'value2', ...);

BETWEEN Operator

The BETWEEN operator is used to select values within a range. The basic syntax of a SELECT statement with the BETWEEN operator is:

SELECT "column_name"
FROM "table_name"
WHERE "column_name" BETWEEN 'value1' AND 'value2';

LIKE Operator

The LIKE operator is used to search for a pattern in a column. The basic syntax of a SELECT statement with the LIKE operator is:

SELECT "column_name"
FROM "table_name"
WHERE "column_name" LIKE {PATTERN};

UNION Operator

The UNION operator is used to combine the result sets of two or more SELECT statements into a single result set. The basic syntax of a SELECT statement with the UNION operator is:

SELECT column1, column2, ... FROM table1 
UNION 
SELECT column1, column2, ... FROM table2;

EXISTS Operator

The EXISTS operator is used to test for the existence of any record in a subquery. The basic syntax of a SELECT statement with the EXISTS operator is:

SELECT column1, column2, ... FROM table_name 
WHERE EXISTS 
(SELECT * FROM table_name WHERE condition);

NOT Operator

The NOT operator is used to negate a condition in a WHERE clause. The basic syntax of a SELECT statement with the NOT operator is:

SELECT column1, column2, ... FROM table_name 
WHERE NOT condition;

ORDER BY Clause

The ORDER BY clause is used to sort the result set by one or more columns. The basic syntax of a SELECT statement with the ORDER BY clause is:

SELECT "column_name"
FROM "table_name"
[WHERE "condition"]
ORDER BY "column_name" [ASC, DESC];

COUNT() Function

The COUNT() function is used to count the number of rows in a table or the number of rows that meet a certain condition. The basic syntax of a SELECT statement with the COUNT() function is:

SELECT COUNT("column_name")
FROM "table_name";

AVG() Function

The AVG() function is used to calculate the average value of a column. The basic syntax of a SELECT statement with the AVG() function is:

SELECT AVG(column_name) FROM table_name;

SUM() Function

The SUM() function is used to calculate the sum of values in a column. The basic syntax of a SELECT statement with the SUM() function is:

SELECT SUM(column_name) FROM table_name;

MAX() Function

The MAX() function is used to find the maximum value in a column. The basic syntax of a SELECT statement with the MAX() function is:

SELECT MAX(column_name) FROM table_name;

MIN() Function

The MIN() function is used to find the minimum value in a column. The basic syntax of a SELECT statement with the MIN() function is:

SELECT MIN(column_name) FROM table_name;

LIMIT Clause

The LIMIT clause is used to limit the number of records returned by a SELECT statement. The basic syntax of a SELECT statement with the LIMIT clause is:

SELECT column1, column2, ... FROM table_name LIMIT n;

GROUP BY Clause

The GROUP BY clause is used to group records based on one or more columns. It is used with aggregate functions such as COUNT, SUM, AVG, MAX, and MIN. The basic syntax of a GROUP BY clause is:

SELECT "column_name1", SUM("column_name2")
FROM "table_name"
GROUP BY "column_name1";

AS Keyword

The AS keyword is used to alias a column or a table in a SELECT statement. The basic syntax of a SELECT statement with the AS keyword is:

SELECT column1 AS alias_name FROM table_name;

HAVING Clause

The HAVING clause is used to filter the result set based on a condition that involves an aggregate function, such as COUNT(), SUM(), AVG(), etc. The basic syntax of a SELECT statement with the HAVING clause is:

SELECT "column_name1", [Function("column_name2")]
FROM "table_name"
[GROUP BY "column_name1"]
HAVING (arithematic function condition);

Create Table Statement

The CREATE TABLE statement is used to create a new table in a database. The basic syntax of a CREATE TABLE statement is:

CREATE TABLE "table_name"
("column 1" "data type for column 1" [column 1 constraint(s)],
"column 2" "data type for column 2" [column 2 constraint(s)],
...
[table constraint(s)]);

Drop-Table Statement

The Drop table statement is used to drop the table from the database. The basic syntax for the drop table statement is:

DROP TABLE "table_name";

Truncate Table Statement

The TRUNCATE TABLE statement is used to delete all the rows from a table. The basic syntax of a TRUNCATE TABLE statement is:

TRUNCATE TABLE "table_name";

INSERT Statement

The INSERT statement is used to insert new records into a table. It specifies the table to insert data into and the values to insert. The basic syntax of an INSERT statement is:

INSERT INTO "table_name" ("column1", "column2", ...)
VALUES ("value1", "value2", ...);

Insert Into Select Statement

The Insert Into Select statement is used to insert values to the selected columns of a table. The basic syntax of Insert Into Select is:

INSERT INTO "table1" ("column1", "column2", ...)
SELECT "column3", "column4", ...
FROM "table2";

Update Statement

The UPDATE statement is used to update existing records in a table. It specifies the table to update, the column to update, and the new value. The basic syntax of an UPDATE statement is:

UPDATE "table_name"
SET "column_1" = [new value]
WHERE "condition";

ALTER TABLE Statement

The ALTER TABLE statement is used to modify the structure of a table in a database. The basic syntax of an ALTER TABLE statement is:

ALTER TABLE table_name ADD column_name datatype;

DELETE statement

The DELETE statement is used to delete existing records from a table. It specifies the table to delete from and the condition that must be met for the record to be deleted. The basic syntax of a DELETE statement is:

DELETE FROM "table_name"
WHERE "condition";

JOIN Operator

The JOIN operator is used to combine rows from two or more tables based on a related column between them. There are several types of JOIN operations, including INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. The basic syntax of a SELECT statement with the JOIN operator is:

SELECT table1.column1, table2.column2 
FROM table1 JOIN table2 
ON table1.column = table2.column;

INNER JOIN with ON clause

The INNER JOIN with ON clause is used to combine rows from two or more tables based on a related column between them. The basic syntax of a SELECT statement with the INNER JOIN with ON clause is:

SELECT table1.column1, table2.column2 
FROM table1 INNER JOIN table2 
ON table1.column = table2.column;

INDEX Statement

The INDEX statement is used to create an index on one or more columns in a table. The basic syntax of an INDEX statement is:

CREATE INDEX index_name ON table_name (column1, column2, ...);

SQL Syntax Checker

When we are writing complex SQL queries, there is a possibility of errors. That’s why Some SQL Syntax Checker tools or software are available online, which help check the syntax of SQL statements for errors and inconsistencies.

By using these SQL Syntax Checker tools, we can increase our productivity because these SQL Syntax Checker tools work by analyzing the structure and grammar of the SQL statements and comparing them to the rules and guidelines of the SQL language. Also, they can check things like missing keywords, incorrect use of clauses, and other syntax errors that could cause the SQL statement to fail.

That’s why most organizations nowadays are using SQL syntax checkers. Because with the help of these tools, they can save developers time and effort by identifying and correcting errors before the SQL statements are executed against a production database. This can help prevent data loss or corruption and improve the performance of the database by ensuring that the SQL statements are well-written and optimized.

Here are some of the SQL Syntax Checker Tools: EverSQL

Conclusion

This article will help you understand important concepts like SQL Syntax and SQL Syntax Checker tools. To learn more about SQL, you can check our other SQL articles, and If you want to improve this article, you can share your inputs in the comment section.

What is the basic syntax?

Basic syntax refers to the rules and principles governing the structure and formatting of code in a programming language. It defines how the code should be written and organized to be correctly interpreted and executed by the computer.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

1 thought on “SQL Syntax Commands”

  1. Also calculate what would be the pension of the employee if the age of the 3 employees is 21, 35, 59 respectively at the age of retirement ?

    Reply

Leave a Comment