SQL Notes
SQL Notes
Date: 2024-01-07
Time: 18:40
Introduction
Structured query language (SQL) is a programming language for storing and processing information in a
relational database. A relational database stores information in tabular form, with rows and columns
representing different data attributes and the various relationships between the data values.
DDL or Data Definition Language actually consists of the SQL commands that can be used to define the
database schema. It simply deals with the description of the database schema and is used to create and modify
the structure of the database objects in the database.
These commands are normally not used by a general user, who should be accessing the database via the
application
Commands :
The SQL commands that deal with the manipulation of data present in the database belong to DML or Data
Manipulation Language and this includes most of the SQL statements. It is the component of the SQL statement
that controls access to data and to the database.
Basically, DCL statements are grouped with DML statements.
Commands :
REVOKE : This command revokes the user's access privileges given by using the GRANT command
Commands
SELECT
SELECT column1,column2,...
FROM table_name;
SELECT DISTINCT
SQL OR
SQL IN
SELECT column_name(s)
FROM table_name
WHERE column_name_ IN (value1, value2, ...);
SQL Between
The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.
Syntax :
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
SQL Like
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
Syntax :
SQL Order By
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
Syntax :
SQL Group By
The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of
customers in each country".
The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to
group the result-set by one or more columns
Syntax :
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
SQL Constraints
Constraints can be specified when the table is created with the CREATE TABLE statement, or after the table is
created with the ALTER TABLE statement.
Syntax :
CREATE TABLE _table_name_ (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
NULL Values