List of SQL Commands

Hello everyone, today we will learn list of SQL query commands.SQL is a domain-concrete language used in programming and designed for managing data held in a relational database management system, or for stream processing in a relational data stream management system.

Commands

ALTER TABLE

ALTER TABLE is used to add, efface/drop or modify columns in the existing table. It is withal used to integrate and drop various constraints on the existing table.

ALTER TABLE table_name ADD column_name datatype;

AND

The AND operator displays a record if all the conditions separated by AND are TRUE.

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ....;

AS

The AS command is used to rename a column or table with an alias.

SELECT UserID AS ID, UserName AS User
FROM Users;

AVG()

SQL Server AVG() function is an aggregate function that returns the average value of a group.

SELECT AVG(Salary) AS AvgSalary FROM Employee;

BETWEEN

The SQL BETWEEN condition allows us to easily test if an expression is within a range of values. The values can be text, date, or numbers.

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

CASE

The case statement in SQL returns a value on a specified condition.

SELECT UserID, age,
CASE
WHEN age > 30 THEN "The age is greater than 30"
WHEN age = 30 THEN "The age is 30"
ELSE "The age is under 30"
END
FROM UserDetails;

COUNT()

The SQL COUNT() function returns the number of rows in a table satisfying the criteria specified in the WHERE clause.

SELECT COUNT(column_name)
FROM table_name
WHERE condition;

CREATE TABLE

CREATE TABLE creates a new table in the database. It allows you to specify the name of the table and the name of each column in the table.

CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);

DELETE

DELETE statements are used to remove rows from a table.

DELETE FROM table_name WHERE condition;

GROUP BY

The GROUP BY clause is a SQL command that is used to group rows that have the same values. The GROUP BY clause is used in the SELECT statement. Optionally it is used in conjunction with aggregate functions to engender summary reports from the database.

SELECT column1, column2
FROM table_name
WHERE [ conditions ]
GROUP BY column1, column2
ORDER BY column1, column2


HAVING

The HAVING Clause enables us to specify conditions that filter which group results appear in the results.

SELECT column1, column2
FROM table1, table2
WHERE [ conditions ]
GROUP BY column1, column2
HAVING [ conditions ]
ORDER BY column1, column2

INNER JOIN

An inner join will combine rows from different tables if the join condition is true.

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

INSERT

The insert command is used for inserting one or more rows into a database table with specified table column values.

INSERT INTO table_name (column_1, column_2, column_3)
VALUES ('value_1', 'value_2', 'value_3');

IS NULL / IS NOT NULL

IS NULL and IS NOT NULL are operators used with the WHERE clause to test for empty values.

SELECT column_name(s)
FROM table_name
WHERE column_name IS NOT NULL;

LIKE

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;

LIMIT

LIMIT is a clause that lets you specify the maximum number of rows the result set will have.

SELECT * FROM Orders LIMIT 30

MAX()

The aggregate function SQL MAX() is used to find the maximum value or highest value of a certain column or expression.

SELECT MAX(column_name)
FROM table_name
WHERE condition;

MIN()

The MIN() function returns the smallest value of the selected column.

SELECT MIN(column_name)
FROM table_name
WHERE condition;

OR

The OR operator displays a record if any of the conditions separated by OR is TRUE.

SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

ORDER BY

The ORDER BY keyword is used to sort the result-set in ascending or descending order.

SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

OUTER JOIN

In an outer join, unmatched rows in one or both tables can be returned.

SELECT column_name(s)
FROM table_1
LEFT JOIN table_2
ON table_1.column_name = table_2.column_name;

ROUND()

The ROUND function returns a number rounded to a certain number of decimal places.

SELECT ROUND(column_name, integer)
FROM table_name;

SELECT

The SQL SELECT statement returns a result set of records, from one or more tables. A SELECT statement retrieves zero or more rows from one or more database tables or database views.

SELECT column1, column2, ...
FROM table_name;

SELECT DISTINCT

The SELECT DISTINCT statement is used to return only distinct (different) values.

SELECT DISTINCT column1, column2, ...
FROM table_name;

SUM

SUM() is a function that takes the name of a column as an argument and returns the sum of all the values in that column.

SELECT SUM(Salary) AS TotalSalary FROM EmployeeDetails;

UPDATE

UPDATE statements allow you to edit rows in a table.

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

WHERE

A WHERE clause in SQL specifies that a SQL statement should only affect rows that meet specified criteria.

SELECT column_name
FROM table_name
WHERE column_name operator value;

WITH

WITH clause lets you store the result of a query in a temporary table using an alias. You can also define multiple temporary tables using a comma and with one instance of the WITH keyword.

WITH <alias_name> AS (sql_subquery_statement)
SELECT column_list FROM <alias_name>[,table_name]
[WHERE <join_condition>]


Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + Mockito simple application with 100% code coverage

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete