Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

SQL Lab Assignment

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Name-Gaurav Arya

MCA 2nd Sem


SQL Lab Assignment
Roll No.-20223037

1. Create table command in SQL


Create table command is used for creating table.
Syntax :- CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
Commands:- create table Students (ID int ,fistname varchar(255),LastName
varchar(255), Roll_No int, Adress varchar(255));

2. INSERT INTO command


Used for inserting value into the table
Syntax :- INSERT INTO table_name (column1,column2, column3, ...)
VALUES (value1, value2, value3, ...);

Output:-
3. Delete command with WHERE clause
Syntax :- DELETE FROM table_name WHERE condition;

Command:- Delete from Students where Id ='4';


select * from Students;
output :-

4. ORDER BY CLAUSE
Syntax :- SELECT column1, column2, ...FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

Command:- select * from Students order by Adress;


Output:-

5. Update command
Syntax:- UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Commands:-
6. SELECT DISTINC command
Insert the same multiple values into the address field then use the select distinct
command
Syntax:- SELECT DISTINCT column1, column2, ...
FROM table_name;
Commands:- select DISTINCT Adress from Students;

Output:-

7. Select Top statement


Synatax:- SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;
->Select * from Students;
This will show the complete table
Output:

Command:- select top 4 * from Students;


Output:-
8. The SQL BETWEEN operator
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;
Commands:-
select * from Students where Roll_No BETWEEN 202 and 205;
output:-

9. LIKE operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column.

Synatax:- SELECT column1, column2, ...


FROM table_name
WHERE columnN LIKE pattern;

Commands:- SELECT * FROM Students


WHERE fistname LIKE 'g%';
Output:-

10. JOINS CLAUSES


A JOIN clause is used to combine rows from two or more tables, based
on a related column between them.
• INNER JOIN
• LEFT(OUTER) JOIN
• RIGHT(OUTER) JOIN
• FULL JOIN
INNER JOIN:- : Returns records that have matching values in both tables.
Syntax:- SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Before using the JOINS,first of all we should have atleast two
table or more then only,we can perform join operations on the
table.
Command:-
SELECT Orders.OrderID,Customers.CustomerName,Customers.ContsactName
from Orders INNER JOIN Customers ON
Orders.customerId=Customers.CustomerID;

“Order” table 1 “ Customer” table 2

Output:-

LEFT JOIN :- The LEFT JOIN keyword returns all records from the left table
(table1), and the matching records from the right table (table2). The result is 0 records
from the right side, if there is no match.
Synatx:- SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Command:- select Orders.OrderID,Customers.CustomerName from Orders LEFT JOIN
Customers ON Orders.customerId=Customers.CustomerID;

Ouput:-
RIGHT JOIN :-
The RIGHT JOIN keyword returns all records from the right table
(table2), and the matching records from the left table (table1). The
result is 0 records from the left side, if there is no match .

Syntax:- SELECT column_name(s)


FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

Commands:- select Orders.OrderID,Customers.CustomerName from Orders RIGHT


JOIN Customers ON Orders.customerId=Customers.CustomerID;

Output:-

FULL JOIN:- The FULL JOIN keyword returns all records when there is a
match in left (table1) or right (table2) table records.

Syntax:- SELECT column_name(s)


FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;

Commands:- select Orders.OrderID, Customers.CustomerName from Orders FULL


OUTER JOIN Customers ON Orders.customerId=Customers.CustomerID
ORDER BY CustomerName;
Output:-

You might also like