0% found this document useful (0 votes)
48 views

SQL SELECT Statement

SQL is a language for managing and manipulating databases. It uses keywords and clauses to perform operations like SELECT, INSERT, UPDATE, DELETE. Key clauses include WHERE, ORDER BY, GROUP BY, HAVING. SQL also supports operators for comparisons, arithmetic, logical operations and aggregate functions to retrieve and analyze data from databases.

Uploaded by

nairdapunk100
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

SQL SELECT Statement

SQL is a language for managing and manipulating databases. It uses keywords and clauses to perform operations like SELECT, INSERT, UPDATE, DELETE. Key clauses include WHERE, ORDER BY, GROUP BY, HAVING. SQL also supports operators for comparisons, arithmetic, logical operations and aggregate functions to retrieve and analyze data from databases.

Uploaded by

nairdapunk100
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL is followed by unique set of rules and guidelines called Syntax.

The following are basic SQL


syntax.
All the SQL statements start with any of the keywords like SELECT, INSERT, UPDATE, DELETE,
ALTER, DROP, CREATE, USE, SHOW and all the statements end with a semicolon (;).
Important point to be noted is that SQL is case insensitive, which means SELECT and select have
same meaning in SQL statements, but MySQL makes difference in table names. So if you are
working with MySQL, then you need to give table names as they exist in the database.

SQL SELECT Statement:


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

SQL DISTINCT Clause:


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

 The SQL DISTINCT keyword is used in conjunction with SELECT statement to


eliminate all the duplicate records and fetching only unique records.
 There may be a situation when you have multiple duplicate records in a table.
While fetching such records, it makes more sense to fetch only unique records
instead of fetching duplicate records.

SQL WHERE Clause:


SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION;

SQL AND/OR Clause:


SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION-1 {AND|OR} CONDITION-2;

SQL IN Clause:
SELECT column1, column2....columnN
FROM table_name
WHERE column_name IN (val-1, val-2,...val-N);

SQL BETWEEN Clause:


SELECT column1, column2....columnN
FROM table_name
WHERE column_name BETWEEN val-1 AND val-2;

SQL LIKE Clause:


SELECT column1, column2....columnN
FROM table_name
WHERE column_name LIKE { PATTERN };

SQL ORDER BY Clause:


SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION
ORDER BY column_name {ASC|DESC};

SQL GROUP BY Clause:


SELECT SUM(column_name)
FROM table_name
WHERE CONDITION
GROUP BY column_name;
SQL COUNT Clause:
SELECT COUNT(column_name)
FROM table_name
WHERE CONDITION;

SQL HAVING Clause:


SELECT SUM(column_name)
FROM table_name
WHERE CONDITION
GROUP BY column_name
HAVING (arithematic function condition);

SQL CREATE TABLE Statement:


CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);

SQL DROP TABLE Statement:


DROP TABLE table_name;

SQL CREATE INDEX Statement :


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

SQL DROP INDEX Statement :


ALTER TABLE table_name
DROP INDEX index_name;

SQL DESC Statement :


DESC table_name;

SQL TRUNCATE TABLE Statement:


TRUNCATE TABLE table_name;

SQL ALTER TABLE Statement:


ALTER TABLE table_name {ADD|DROP|MODIFY} column_name {data_ype};

SQL ALTER TABLE Statement (Rename) :


ALTER TABLE table_name RENAME TO new_table_name;

SQL INSERT INTO Statement:


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

SQL UPDATE Statement:


UPDATE table_name
SET column1 = value1, column2 = value2....columnN=valueN
[ WHERE CONDITION ];

SQL DELETE Statement:


DELETE FROM table_name
WHERE {CONDITION};
SQL CREATE DATABASE Statement:
CREATE DATABASE database_name;

SQL DROP DATABASE Statement:


DROP DATABASE database_name;

SQL USE Statement:


USE database_name;

SQL COMMIT Statement:


COMMIT;

SQL ROLLBACK Statement:


ROLLBACK;

What is an Operator in SQL?


An operator is a reserved word or a character used primarily in an SQL statement's WHERE
clause to perform operation(s), such as comparisons and arithmetic operations.
Operators are used to specify conditions in an SQL statement and to serve as conjunctions for
multiple conditions in a statement.
 Arithmetic operators
 Comparison operators
 Logical operators
 Operators used to negate conditions

SQL Arithmetic Operators:


Assume variable a holds 10 and variable b holds 20, then:
Operator Description Example
Addition - Adds values on either side of
+ a + b will give 30
the operator
Subtraction - Subtracts right hand
- a - b will give -10
operand from left hand operand
Multiplication - Multiplies values on
* a * b will give 200
either side of the operator
Division - Divides left hand operand by
/ b / a will give 2
right hand operand
Modulus - Divides left hand operand by
% right hand operand and returns b % a will give 0
remainder

SQL Comparison Operators:


Assume variable a holds 10 and variable b holds 20, then:
Operator Description Example
Checks if the values of two operands are
= equal or not, if yes then condition (a = b) is not true.
becomes true.
Checks if the values of two operands are
!= equal or not, if values are not equal then (a != b) is true.
condition becomes true.
Checks if the values of two operands are
<> equal or not, if values are not equal then (a <> b) is true.
condition becomes true.
Checks if the value of left operand is
> greater than the value of right operand, if (a > b) is not true.
yes then condition becomes true.
Checks if the value of left operand is less
< than the value of right operand, if yes (a < b) is true.
then condition becomes true.
Checks if the value of left operand is
greater than or equal to the value of right
>= (a >= b) is not true.
operand, if yes then condition becomes
true.
Checks if the value of left operand is less
than or equal to the value of right
<= (a <= b) is true.
operand, if yes then condition becomes
true.
Checks if the value of left operand is not
!< less than the value of right operand, if yes (a !< b) is false.
then condition becomes true.
Checks if the value of left operand is not
!> greater than the value of right operand, if (a !> b) is true.
yes then condition becomes true.

SQL Logical Operators:


Here is a list of all the logical operators available in SQL.
Operator Description
The ALL operator is used to compare a value to all values in another value
ALL
set.
The AND operator allows the existence of multiple conditions in an SQL
AND
statement's WHERE clause.
The ANY operator is used to compare a value to any applicable value in the
ANY
list according to the condition.
The BETWEEN operator is used to search for values that are within a set of
BETWEEN
values, given the minimum value and the maximum value.
The EXISTS operator is used to search for the presence of a row in a
EXISTS
specified table that meets certain criteria.
The IN operator is used to compare a value to a list of literal values that
IN
have been specified.
The LIKE operator is used to compare a value to similar values using
LIKE
wildcard operators.
The NOT operator reverses the meaning of the logical operator with which it
NOT is used. Eg: NOT EXISTS, NOT BETWEEN, NOT IN, etc. This is a negate
operator.
The OR operator is used to combine multiple conditions in an SQL
OR
statement's WHERE clause.
IS NULL The NULL operator is used to compare a value with a NULL value.
The UNIQUE operator searches every row of a specified table for
UNIQUE
uniqueness (no duplicates).

SQL Aggregate Functions


SQL aggregate functions return a single value, calculated from values in a column.
Useful aggregate functions:
 AVG() - Returns the average value
 COUNT() - Returns the number of rows
 FIRST() - Returns the first value
 LAST() - Returns the last value
 MAX() - Returns the largest value
 MIN() - Returns the smallest value
 SUM() - Returns the sum
SQL Scalar functions
SQL scalar functions return a single value, based on the input value.
Useful scalar functions:
 UCASE() - Converts a field to upper case
 LCASE() - Converts a field to lower case
 MID() - Extract characters from a text field
 LEN() - Returns the length of a text field
 ROUND() - Rounds a numeric field to the number of decimals specified
 NOW() - Returns the current system date and time
 FORMAT() - Formats how a field is to be displayed

Subquery

A Subquery or Inner query or Nested query is a query within another SQL query and embedded
within the WHERE clause.

A subquery is used to return data that will be used in the main query as a condition to further
restrict the data to be retrieved.

Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with
the operators like =, <, >, >=, <=, IN, BETWEEN etc.

There are a few rules that subqueries must follow:

 Subqueries must be enclosed within parentheses.


 A subquery can have only one column in the SELECT clause, unless multiple columns are
in the main query for the subquery to compare its selected columns.
 An ORDER BY cannot be used in a subquery, although the main query can use an ORDER
BY. The GROUP BY can be used to perform the same function as the ORDER BY in a
subquery.
 Subqueries that return more than one row can only be used with multiple value
operators, such as the IN operator.
 The SELECT list cannot include any references to values that evaluate to a BLOB, ARRAY,
CLOB, or NCLOB.
 A subquery cannot be immediately enclosed in a set function.
 The BETWEEN operator cannot be used with a subquery; however, the BETWEEN
operator can be used within the subquery.

SELECT column_name [, column_name ]


FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE])

You might also like