SQL
SQL
DATATYPES
1. CHARACTER STRING DATATYPE
a. CHAR(n)
b. NCHAR(n)
c. VARCHAR(n)
2. DATE AND TIME DATATYPE
a. DATE (2022-05-13)
b. DATETIME (2022-05-1303:34:56)
3. NUMERIC DATATYPE
a. INT
4. BINARY DATATYPE
a. BIT
b. BINARY(n)
c. VARBINARY(n)
SQL stands for Structured Query Language and is a computer
language that we use to interact with a relational database.
It is a tool used for organizing, managing,
and retrieving archived data from a computer database. The
original name was given by IBM as Structured English Query
Language, abbreviated by the acronym SEQUEL.
TYPES OF SQL COMMAND
1. DATA DEFINITION LANGUAGE (DDL): changes the
structure of the table and database. It permanently
stores the changes in the database.
a. CREATE ()
b. ALTER
c. DROP
d. TRUNCATE
2. DATA MANUIPLATION LANGUAGE (DML)
3. DATA CONTROL LANGUAGE (DCL)
4. TRANSACTION CONTROL LANG (TCL)
CONSTRAINTS
Constraints enforces rules on a table whenever
rows/records are inserted, updated and deleted from the
table.
They prevent deletion of table if there are dependence
from other tables.
Constraints can be applied during creation of tables or after
tables creation by using the alter command
Types of constraints
NOT NULL Specifies that a column must have some
value
UNIQUE Specifies that a column must have a
unique value
PRIMARY KEY Specifies a column or set of column that
uniquely identifies a row/record. It
doesn’t not allow a null value
FOREIGN KEY It is a column in a table that references a
column in another.
DML COMMAND
This language constitutes the statements that are used to
manipulate data in a table.
1. INSERT
2. UPDATE
3. DELETE
4. SELECT
INSERT COMMAND: this is used to insert data/records into the
database table.
Syntax
INSERT INTO <tablename> (fieldname1,fieldname2, …..)
VALUES (value1, value2, …..)
EXAMPLE
INSERT INTO dept (deptno, deptname) VALUES
(20,’marketing’)
NOTE: if we want to insert values in all the columns, then
there is no need to specify column/ field name. but the order
of column values should be sync with the column name
EXAMPLE:
INSERT INTO dept VALUES (20, ‘marketing’)
UPDATE STATEMENT:
Syntax
UPDATE <tablename>
SET <fieldname> = <value>
WHERE <condition>
Example
UPDATE dept
SET dept_name = ‘marketing’
WHERE dept-no = 30
UPDATE STATEMENT WITHOUT A WHERE CLAUSE
UPDATE dept SET dept_name = “marketing”
The above statement will change the whole dept_name to
marketing.
Example:
INSERT INTO dept2 (deptno,deptname,location)
SELECT * FROM dept1
WHERE deptno > 20
INSERT INTO dept2 (deptno,deptname,location)
SELECT * FROM dept1 (NOTE: THIS COMMAND WILL INSERT ALL THE RECORDS FROM
dept1)
SELECT STATEMENT
SELECT TOP(5) * FROM dept
SELECT TOP (10) firstname, lastname FROM dept
COMPARISON OPERATOR
Used in WHERE conditions to fetch results from table
(=, !=, <,>, >=, <=, LIKE, BETWEEN, IN)
SELECT * FROM dept
WHERE salary BETWEEN 2000 AND 4000
SQL JOINS
A JOIN clause is used to fetch data from two or more data
tables, based on the JOIN conditons. It is used to combine
rows from one or more tables, based on a related columns
between them
TYPES OF JOIN
CROSS JOIN
SELF JOIN
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL OUTER JOIN
INNER JOIN: this fetches records that have matching values in
both tables
Syntax
SELECT <tablename.columname,tablename.columname,…..>
FROM <tablename1>
INNER JOIN <tablename2>
ON tablename1.matchingcolumname =
tablename2.matchingcolumname
NOTE: the two tables you want to join must have common
values or column between them.
Example
SELECT emp.employeeID, order.orderID, order.orderDate
FROM emp
INNER JOIN orders
ON emp.employeeID = order.employeeID
ORDER BY employeeID
LEFT JOIN: this returns all the rows/records of the table on the
left side of the join and matches the rows/records for the
table on the right side of the join. For rows which there are no
matching records/rows on the right side, the result-set will
contain NULL. LEFT JOIN is also known as LEFT OUTER JOIN.
Syntax
SELECT <column_list>
FROM <tablename1>
LEFT JOIN <tablename2>
ON tablename1.matchingcolumname =
tablename2.matchingcolumname
Example
SELECT emp.employeeID, order.orderID, order.orderDate
FROM emp
INNER JOIN orders
ON emp.employeeID = order.employeeID
ORDER BY employeeID
RIGHT JOIN: is similar to LEFT JOIN. This returns all the
rows/records of the table on the right side of the join and
matching rows/records for the table on the left side of the join