0% found this document useful (0 votes)
60 views24 pages

Babu Banarasi Das University: Subject Name-Subject Code

This document contains 25 SQL queries with explanations and examples. The queries cover topics such as creating databases and tables, altering tables, adding and dropping columns and constraints, aggregation, filtering with comparison operators, and more. Examples are provided for each type of SQL statement to demonstrate its syntax and usage.

Uploaded by

so gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views24 pages

Babu Banarasi Das University: Subject Name-Subject Code

This document contains 25 SQL queries with explanations and examples. The queries cover topics such as creating databases and tables, altering tables, adding and dropping columns and constraints, aggregation, filtering with comparison operators, and more. Examples are provided for each type of SQL statement to demonstrate its syntax and usage.

Uploaded by

so gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

BABU BANARASI DAS UNIVERSITY

Subject name-

Subject code-

Submitted to- Submitted by-


INDEX
S. No. Contents Signature Remarks
INDEX
S. No. Contents Signature Remarks
INDEX
S. No. Contents Signature Remarks
INDEX
S. No. Contents Signature Remarks
SQL Queries
1) Create database-

Syntax-
CREATE DATABASE databasename;

Example
CREATE DATABASE testDB;

Output-

The following SQL statement creates a database called "testDB".

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.

PersonID LastName FirstName Address City


3) ALTER TABLE - ADD Column

Syntax-

ALTER TABLE table_name
ADD column_name datatype;

Example-
ALTER TABLE Customers
ADD Email varchar(255);

Output-

PersonID LastName FirstName Address City Email

4) ALTER TABLE - DROP column

Note: that some database systems don't allow deleting a column

Syntax-

ALTER TABLE table_name
DROP COLUMN column_name;

Example-
ALTER TABLE Customers
DROP COLUMN Email;

Output-
1 column dropped.

PersonID LastName FirstName Address City

5) ALTER TABLE - ALTER/MODIFY COLUMN-


Syntax-
ALTER TABLE table_name
MODIFY column_name datatype;

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.

6) SQL NOT NULL constraints on CREATE TABLE-

NOT NULL - Ensures that a column cannot have a NULL value.

EXAMPLE-

CREATE TABLE Persons (
    ID int NOT NULL,
     LastName varchar(255) NOT NULL,
    FirstName varchar(255) NOT NULL,
    Age int
);

7) SQL NOT NULL on ALTER TABLE

Example-

ALTER TABLE Persons
MODIFY Age int NOT NULL;

8) SQL UNIQUE Constraint on CREATE TABLE-

UNIQUE - Ensures that all values in a column are different

Example-
CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
     CONSTRAINT UC_Person UNIQUE (ID,LastName)
);

9) DROP a UNIQUE Constraint-


Example-
ALTER TABLE Persons
DROP CONSTRAINT UC_Person;

10) SQL PRIMARY KEY on CREATE TABLE


PRIMARY KEY is a combination of a NOT NULL and UNIQUE. Uniquely
identifies each row in a table.
Example-
CREATE TABLE Persons (
    ID int NOT NULL PRIMARY KEY,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int
);

11) SQL PRIMARY KEY on ALTER TABLE


Example-
ALTER TABLE Persons
ADD PRIMARY KEY (ID);

12) DROP a PRIMARY KEY Constraint-

Example-

ALTER TABLE Persons
DROP PRIMARY KEY;

13) SQL FOREIGN KEY on CREATE TABLE


Example-
CREATE TABLE Orders (
    OrderID int NOT NULL PRIMARY KEY,
    OrderNumber int NOT NULL,
    PersonID int FOREIGN KEY REFERENCES Persons(PersonID)
);

14) SQL FOREIGN KEY on ALTER TABLE


Example-
ALTER TABLE Orders
ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);

15) CREATE VIEW-


Syntax-
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Example-
CREATE VIEW [Brazil Customers] AS
SELECT CustomerName, ContactName
FROM Customers
WHERE Country = "Brazil";

How to see the view-

SELECT * FROM [Brazil Customers];

Output-
Number of Records: 4

CustomerName ContactName

Comércio Mineiro Pedro Afonso

Familia Arquibaldo Aria Cruz

Gourmet Lanchonetes André Fonseca

Hanari Carnes Mario Pontes


16) DROP TABLE-
Syntax-
DROP TABLE table_name;

Example-
DROP TABLE Shippers;

17) TRUNCATE TABLE-

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;

18) DROP DATABASE –


Syntax-
DROP DATABASE databasename;

Example-
DROP DATABASE testDB;

19) SELECT(multiple columns)-


Syntax-
SELECT column1,  column2, ...
FROM table_name;

Example-
SELECT CustomerName, City FROM Customers;

Output-
Number of Records: 4
CustomerName City

20) Alfreds Futterkiste Berlin SELECT(full table)-


Ana México D.F.

Antonio Moreno México D.F.

Horn London

Syntax-
SELECT * FROM table_name;

Example-
SELECT * FROM Customers;

OUTPUT-

Number of Records: 5

Custome Customer ContactName Address City Country


rID Name

1 Alfreds Maria Anders Obere Str. 57 Berlin Germany


Futterkiste

2 Ana Trujillo Ana Trujillo Avda. de la México Mexico


Constitución D.F.
2222

3 Antonio Antonio Moreno Mataderos México Mexico


2312 D.F.

4 Horn Thomas Hardy 120 Hanover London UK


Sq.

5 Berglunds Christina Berglund Berguvsvägen Luleå Sweden


8

21) SELECT DISTINCT-

The SELECT DISTINCT statement is used to return only distinct (different)


values. Inside a table, a column often contains many duplicate values;
and sometimes you only want to list the different (distinct) values.
Syntax-

SELECT DISTINCT column1,  column2, ...


FROM table_name;

Example-
SELECT Country FROM Customers;

Output-
Number of Records: 4

Country

Germany

Mexico

UK

Sweden

22) SQL WHERE Clause-

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-

CustomerID Custom ContactNa Address City Country


erNam me
e

2 Ana Ana Trujillo Avda. de Méxic Mexico


Trujillo la o
Empare Constituci D.F.
dados y ón 2222
helados

3 Antonio Antonio Mataderos Méxic Mexico


Moreno Moreno 2312 o
Taquerí D.F.
a

Syntax-
SELECT * FROM tablename

WHERE columnName=number/text; //condition of ‘=’

Example-
SELECT * FROM Products

WHERE Price = 18;

Output-

Number of Records: 4

Product ProductName SupplierID CategoryID Unit Price


ID

1 Chais 1 1 10 boxes x 18
20 bags

35 Steeleye Stout 16 1 24 - 12 oz 18
bottles

39 Chartreuse verte 18 1 750 cc per 18


bottle
76 Lakkalikööri 23 1 500 ml 18

24) ‘GREATER THAN’ (>) Operators in The WHERE Clause-


Syntax-
SELECT * FROM tablename
WHERE columnName>number; //condition of ‘>’

Example-
SELECT * FROM Products
WHERE Price > 30;

Output-
Number of Records: 4

ProductID ProductName SupplierI CategoryID Unit Price


D

8 Northwoods 3 2 12 - 12 oz 40
Cranberry Sauce jars

9 Mishi Kobe Niku 4 6 18 - 500 g 97


pkgs.

10 Ikura 4 8 12 - 200 31
ml jars

12 Queso Manchego La 5 4 10 - 500 g 38


Pastora pkgs.

25) ‘LESS THAN’ (<) Operators in The WHERE Clause-


SYNTAX-
SELECT * FROM tablename
WHERE columnName<number; //condition of ‘<’

EXAMPLE-
SELECT * FROM Products
WHERE Price < 30;
OUTPUT-
Number of Records: 4

ProductID ProductName SupplierI CategoryI Unit Price


D D

1 Chais 1 1 10 boxes x 18
20 bags

2 Chang 1 1 24 - 12 oz 19
bottles

3 Aniseed Syrup 1 2 12 - 550 ml 10


bottles

4 Chef Anton's Cajun 2 2 48 - 6 oz 22


Seasoning jars

26) ‘GREATER THAN OR EQUAL TO’ (>=) Operators in The WHERE


Clause-
SYNTAX-
SELECT * FROM tablename
WHERE columnName>=number; //condition of ‘>=’

EXAMPLE-
SELECT * FROM Products

WHERE Price >= 30;


OUTPUT-

ProductID ProductName SupplierID CategoryID Unit Pric


e

7 Uncle Bob's Organic Dried 3 7 12 - 1 lb 30


Pears pkgs.

8 Northwoods Cranberry Sauce 3 2 12 - 12 oz 40


27) jars ‘LE
SS
9 Mishi Kobe Niku 4 6 18 - 500 g 97
pkgs.

10 Ikura 4 8 12 - 200 ml 31
jars

THAN OR EQUAL TO’ (<=) Operators in The WHERE Clause-


SYNTAX-
SELECT * FROM tablename
WHERE columnName<=number; //condition of <‘=’

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

WHERE columnName<>number; //condition of ‘<>’

EXAMPLE-
SELECT * FROM Products
WHERE Price <> 18;

OUTPUT-
Number of Records: 2

ProductID ProductName SupplierID CategoryI Unit Price


D

2 Chang 1 1 24 - 12 oz 19
bottles

3 Aniseed Syrup 1 2 12 - 550 ml 10


bottles

29) AND operator-


Syntax-
SELECT column1,  column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

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

SELECT column1,  column2, ...


FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

EXAMPLE-
SELECT * FROM Customers
WHERE City='Berlin' OR City='München';

OUTPUT-
Number of Records: 2

Customer Custom ContactNa Addre City PostalCode Country Email


ID erName me ss

1 Alfreds Maria Obere Berli 12209 Germany null


Futterki Anders Str. 57 n
ste

25 Franken Peter Berlin Mün 80805 Germany null


versand Franken er chen
Platz
43

31) NOT operator-

Syntax-
SELECT column1,  column2, ...
FROM table_name
WHERE NOT condition;
EXAMPLE-
SELECT * FROM Customers
WHERE NOT Country='Germany';

OUTPUT-
Number of Records: 80

Customer Custo ContactNam Address City Postal Country Email


ID merNa e Code
me

2 Ana Ana Trujillo Avda. México 05021 Mexico null


Trujillo 2222 D.F.

3 Antonio Antonio Mataderos México 05023 Mexico null


Moreno 2312 D.F.

32)LIKE operator-

SYNTAX-
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;

EXAMPLE-
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';

(NOTE- This statement selects all customers with a CustomerName


starting with "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

Product Product SupplierID Category Unit Price


ID Name ID

1 Chais 1 1 10 boxes x 20 bags 18

2 Chang 1 1 24 - 12 oz bottles 19

3 Aniseed 1 2 12 - 550 ml bottles 10


Syrup

15 Genen 6 2 24 - 250 ml bottles 15.5


Shouyu

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)

You might also like