Basic Commands in SQL

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Basic Commands in SQL:

Command Syntax Description


ALTER table ALTER TABLE table_name ADD It is used to add columns
column_name datatype; to a table in a database
AND SELECT column_name(s)FROM It is an operator that is
table_nameWHERE column_1 = used to combine two
value_1 AND column_2 = value_2; conditions
AS SELECT column_name AS It is an keyword in SQL
‘Alias’FROM table_name; that is used to rename a
column or table using an
alias name
AVG SELECT AVG(column_name)FROM It is used to aggregate a
table_name; numeric column and return
its average
BETWEEN SELECT column_name(s)FROM It is an operator used to
table_nameWHERE column_name filter the result within a
BETWEEN value_1 AND value_2; certain range
CASE SELECT column_name,CASEWHEN It is a statement used to
condition THEN ‘Result_1’WHEN create different outputs
condition THEN ‘Result_2’ELSE inside a SELECT
‘Result_3’ENDFROM table_name; statement
COUNT SELECT It is a function that takes
COUNT(column_name)FROM the name of a column as
table_name; argument and counts the
number of rows when the
column is not NULL
Create TABLE CREATE TABLE table_name It is used to create a new
( column_1 datatype, column_2 table in a database and
datatype, column_3 datatype); specify the name of the
table and columns inside it
DELETE DELETE FROM table_nameWHERE It is used to remove the
some_column = some_value; rows from a table
GROUP BY SELECT column_name, It is an clause in SQL used
COUNT(*)FROM table_nameGROUP for aggregate functions in
BY column_name; collaboration with the
SELECT statement
HAVING SELECT column_name, It is used in SQL because
COUNT(*)FROM table_nameGROUP the WHERE keyword
BY column_nameHAVING COUNT(*) cannot be used in
> value; aggregating functions
INNER JOIN SELECT column_name(s)FROM It is used to combine rows
table_1JOIN table_2 ON from different tables if the
table_1.column_name = Join condition goes TRUE
table_2.column_name;
INSERT INSERT INTO table_name It is used to add new rows
(column_1, column_2, column_3) to a table
VALUES (value_1, ‘value_2’,
value_3);
IS NULL/ IS NOT SELECT column_name(s)FROM It is an operator used with
NULL table_nameWHERE column_name IS the WHERE clause to
NULL; check for the empty values
LIKE SELECT column_name(s)FROM It is a special operator
table_nameWHERE column_name used with the WHERE
LIKE pattern; clause to search for a
specific pattern in a
column
LIMIT SELECT column_name(s)FROM It is a clause to specify the
table_nameLIMIT number; maximum number of rows
the result set must have
MAX SELECT MAX(column_name)FROM It is a function that takes
table_name; number of columns as an
argument and return the
largest value among them
MIN SELECT MIN(column_name)FROM It is a function that takes
table_name; number of columns as an
argument and return the
smallest value among
them
OR SELECT column_nameFROM It is an operator that is
table_nameWHERE column_name = used to filter the result set
value_1 OR column_name = to contain only the rows
value_2; where either condition is
TRUE
ORDER BY SELECT column_nameFROM It is a clause used to sort
table_nameORDER BY the result set by a
column_name ASC | DESC; particular column either
numerically or
alphabetically
OUTER JOIN SELECT column_name(s)FROM It is sued to combine rows
table_1LEFT JOIN table_2 ON from different tables even if
table_1.column_name = the condition is NOT TRUE
table_2.column_name;
ROUND SELECT ROUND(column_name, It is a function that takes
integer)FROM table_name; the column name and a
integer as an argument,
and rounds the values in a
column to the number of
decimal places specified
by an integer
SELECT SELECT column_name FROM It is a statement that is
table_name; used to fetch data from a
database
SELECT SELECT DISTINCT It is used to specify that
DISTINCT column_nameFROM table_name; the statement is a query
which returns unique
values in specified
columns
SUM SELECT SUM(column_name)FROM It is function used to return
table_name; sum of values from a
particular column
UPDATE UPDATE table_nameSET It is used to edit rows in a
some_column = some_valueWHERE atble
some_column = some_value;
WHERE SELECT column_name(s)FROM It is a clause used to filter
table_nameWHERE column_name the result set to include the
operator value; rows which where the
condition is TRUE
WITH WITH temporary_name AS (SELECT It is used to store the result
*FROM table_name)SELECT *FROM of a particular query in a
temporary_nameWHERE temporary table using an
column_name operator value; alias

You might also like