Babu Banarasi Das University: Subject Name-Subject Code
Babu Banarasi Das University: Subject Name-Subject Code
Subject name-
Subject code-
Syntax-
CREATE DATABASE databasename;
Example
CREATE DATABASE testDB;
Output-
2) Create table-
Syntax-
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
Example-
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Output-
1 table created.
Syntax-
ALTER TABLE table_name
ADD column_name datatype;
Example-
ALTER TABLE Customers
ADD Email varchar(255);
Output-
Syntax-
ALTER TABLE table_name
DROP COLUMN column_name;
Example-
ALTER TABLE Customers
DROP COLUMN Email;
Output-
1 column dropped.
Example-
ALTER TABLE Persons
MODIFY DateOfBirth year;
Output-
The "DateOfBirth" column is now of type year and is going to hold a year in a
two- or four-digit format.
EXAMPLE-
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
Example-
ALTER TABLE Persons
MODIFY Age int NOT NULL;
Example-
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT UC_Person UNIQUE (ID,LastName)
);
Example-
ALTER TABLE Persons
DROP PRIMARY KEY;
Example-
CREATE VIEW [Brazil Customers] AS
SELECT CustomerName, ContactName
FROM Customers
WHERE Country = "Brazil";
SELECT * FROM [Brazil Customers];
Output-
Number of Records: 4
CustomerName ContactName
Example-
DROP TABLE Shippers;
The TRUNCATE TABLE statement is used to delete the data inside a table,
but not the table itself.
Syntax-
TRUNCATE TABLE table_name;
EXAMPLE-
TRUNCATE TABLE Shippers;
Example-
DROP DATABASE testDB;
Example-
SELECT CustomerName, City FROM Customers;
Output-
Number of Records: 4
CustomerName City
Horn London
Syntax-
SELECT * FROM table_name;
Example-
SELECT * FROM Customers;
OUTPUT-
Number of Records: 5
Example-
SELECT Country FROM Customers;
Output-
Number of Records: 4
Country
Germany
Mexico
UK
Sweden
Syntax-
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example-
SELECT * FROM Customers
WHERE Country='Mexico';
Output-
Number of Records: 2
23) ‘EQUAL TO’ (=)Operators in The WHERE Clause-
Syntax-
SELECT * FROM tablename
Example-
SELECT * FROM Products
Output-
Number of Records: 4
1 Chais 1 1 10 boxes x 18
20 bags
35 Steeleye Stout 16 1 24 - 12 oz 18
bottles
Example-
SELECT * FROM Products
WHERE Price > 30;
Output-
Number of Records: 4
8 Northwoods 3 2 12 - 12 oz 40
Cranberry Sauce jars
10 Ikura 4 8 12 - 200 31
ml jars
EXAMPLE-
SELECT * FROM Products
WHERE Price < 30;
OUTPUT-
Number of Records: 4
1 Chais 1 1 10 boxes x 18
20 bags
2 Chang 1 1 24 - 12 oz 19
bottles
EXAMPLE-
SELECT * FROM Products
10 Ikura 4 8 12 - 200 ml 31
jars
EXAMPLE-
SELECT * FROM Products
WHERE Price <= 30;
OUTPUT-
1 Chais 1 1 10 boxes x 20 18
bags
6 Grandma's 3 2 12 - 8 oz jars 25
Boysenberry
Spread
7 Uncle Bob's 3 7 12 - 1 lb 30
Organic Dried pkgs.
Pears
28) ‘NOT EQUAL’ (<>/!=) Operators in The WHERE Clause-
SYNTAX-
SELECT * FROM tablename
EXAMPLE-
SELECT * FROM Products
WHERE Price <> 18;
OUTPUT-
Number of Records: 2
2 Chang 1 1 24 - 12 oz 19
bottles
EXAMPLE-
SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';
OUTPUT-
Number of Records: 1
CustomerI CustomerNam ContactNam Address City PostalCod Country Email
D e e e
30)OR operator-
1 Alfreds Maria Anders Obere Berli 12209 Germany null
Syntax- Futterkiste Str. 57 n
EXAMPLE-
SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
OUTPUT-
Number of Records: 2
Syntax-
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
EXAMPLE-
SELECT * FROM Customers
WHERE NOT Country='Germany';
OUTPUT-
Number of Records: 80
32)LIKE operator-
SYNTAX-
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
EXAMPLE-
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';
OUTPUT-
Number of Records: 3
Custo Customer Contact Address City Country
merID Name Name
Custo CustomerN ContactNa Address City Country Em
1 Alfreds Maria Obere Berlin Germany
merI ame me ail
Anders Str. 57
D
4 Horn Thomas 120 Londo UK
1 Alfreds Maria Obere Str. Berlin Germany null
Hardy Hanover n
Futterkiste Anders 57
Sq.
2 Ana Trujillo Ana Trujillo Avda. de México Mexico null
6 Blauer See Hanna Forsterst Mannh Germany
2222 D.F.
Moos r. 57 eim
3 Antonio Antonio Mataderos México Mexico null
Moreno Moreno 2312 D.F.
33)IN operator-
SYNTAX-
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
or:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
EXAMPLE-
SELECT * FROM Customers
WHERE Country IN ('Germany', 'UK');
OUTPUT-
34)BETWEEN operator-
SYNTAX-
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
EXAMPLE-
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
OUTPUT-
Number of Records: 4
2 Chang 1 1 24 - 12 oz bottles 19
35)
22) INSERT table-
23) DELETE table-
24) UPDATE table-
25) ORDER BY query-
26) MIN
27) MAX queries-
28) COUNT-
29) AVG-
30) SUM-
31) WILDCARDS-
32) ALIASES-
33) INNER JOIN-
34) LEFT JOIN-
35) RIGHT JOIN-
36) FULL JOIN-
37) SELF JOIN-
38) UNION-
39) GROUP BY-
40) HAVING-
41) EXISTS-
42) ANY-
43) ALL-
44) SELECT INTO-
45) INSERT INTO SELECT-
46) SQL CASE-
47) SQL COMMENTS-
48)