0% found this document useful (0 votes)
5 views13 pages

SQL

Uploaded by

alphamaleomega34
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
5 views13 pages

SQL

Uploaded by

alphamaleomega34
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 13

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)

DATA DEFINITION LANGUAGE (DDL):


CREATE DATABASE: this statement is used to create a new
database object.
Syntax
CREATE DATABASE <database_name>
CREATE TABLE: this is used to create a new table in a
selected database.
Syntax
CREATE TABLE <table_name> (
Column A datatype,
Column B datatype,
Column C datatype
)
Open a new query.
Create a database name market_business.
Create a two table name product and order
o Product
 Product_ID
 Productname
 Quantity
o Order_
 Order_ID
 Orderlocation
DROP : this is used to remove a table from a database. Or
used to delete a database.
Syntax
DROP TABLE <tablename>

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.

ALTER COMMAND: this allows you to alter table, column


and constraints.
Syntax

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.

DELETE STATEMENT: This command is used to delete


rows/record from database table. Delete statement can be
executed with or without WHERE condition. Execution of
delete command without WHERE condition will remove all the
records from the table
Syntax
DELETE FROM <tablename>
WHERE <condition>
DELETE STATEMENT WITHOUT A WHERE CLAUSE
DELETE FROM <tablename>
Example:
DELETE FROM dept2
WHERE deptno = 20

DELETE FROM dept2

INSERT-AS-SELECT statement: this allows you to insert into a


table inputs from another table. Records from one table will
be inserted into another table.
Syntax
INSERT INTO <tablename1> (column1,column2….)
SELECT * FROM <tablename2>
WHERE <condition>

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

ORDERING RESULTS USING “ORDER BY”


This is used alongside WHERE clause to display the specified
columns in ascending order or descending order.
SELECT firstname, deptno, hiredate
FROM employee
ORDER BY deptno ASC, hiredate DESC
SELECT * FROM dept
ORDER BY deptno DESC
FILTERING USING LOGICAL OPERATOR
Logical operators (AND, OR and NOT) used in WHERE
conditions to join more than two queries. It combines the
results of two or more conditions to produce single result.
USING THE AND LOGICAL OPERATOR
Used to combine two conditions and it fetches the result
which satisfies both conditions.
Example
SELECT firstname,deptno,salary FROM employee
WHERE salary >20000 AND salary < 55000
USING THE NOT LOGICAL OPERATOR
SELECT * FROM dept
WHERE loc NOT IN (“enugu”)

COMPARISON OPERATOR
Used in WHERE conditions to fetch results from table
(=, !=, <,>, >=, <=, LIKE, BETWEEN, IN)
SELECT * FROM dept
WHERE salary BETWEEN 2000 AND 4000

IN operator is used to test whether or not a value is in the list


values provided after the keyword IN
SELECT * FROM dept
WHERE deptno IN (001)
Note: equal (=) operator does not work with a NULL value
Example:
SELECT * FROM dept
WHERE salary = NULL
The statement above won’t display anything.
BETTER USE
SELECT * FROM dept
WHERE salary IS NULL
OR
SELECT * FORM dept
WHERE salary IS NOT NULL
USING LIKE OPERATOR: LIKE condition is used to perform wild
card searches of valid search string value.
We use:
% to denotes zero or many characters
- to denote one character
SELECT * FROM dept
WHERE loc LIKE “c%i”
CASE EXPRESSION
Used as a Type of IF-THEN-ELSE statement logic to SQL
Syntax:
SELECT CASE (column_name)
WHEN condition 1 THEN result1
WHEN condition 2 THEN result2
ELSE result
END
FROM <tablename>
Example:
SELECT CASE (address)
WHEN “obiagu” THEN “enugu”
WHEN “GRA” THEN “awka”
ELSE “no idea”
END
FROM dept

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

You might also like