SQL(Structure Query Language)
A. (DML) Data Manipulation Language - Non Query
B. (DDL) Data Definition Language - Execute Query
- The ALTER TABLE statement is also used to add and drop various constraints on an existing table.
ADD Email varchar(255);
DROP COLUMN Email;
RENAME COLUMN old_name to new_name;
ALTER COLUMN column_name datatype;
C. Database Clauses
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
SELECT Name FROM Emp WHERE Salary NOT BETWEEN 30000 AND 45000;
SELECT
country,
COUNT
(*)
FROM
box_office
GROUP
BY
country;
SELECT
movie,
city,
gross
FROM
box_office
ORDER
BY
gross;
SELECT
movie
FROM
box_office
GROUP
BY
movie
ORDER
BY
movie
ASC
;
Find the Fname, and Lname of the Employees who have a Salary equal to 30000, 40000, or 25000.
SELECT Name FROM Emp WHERE Salary IN (30000, 40000, 25000);
- The EXISTS operator returns TRUE if the subquery returns one or more records.
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID = Suppliers.supplierID AND Price < 20);
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
D. Aggregate Functions (SQL Functions)
E. Database Alias
Aliases are the temporary names given to tables or columns for the purpose of a particular SQL query. It is used when the name of a column or table is used other than its original name, but the modified name is only temporary.
- Aliases are created to make table or column names more readable.
- The renaming is just a temporary change and the table name does not change in the original database.
- Aliases are useful when table or column names are big or not very readable.
- These are preferred when there is more than one table involved in a query.
SELECT CustomerID AS SSN FROM Customer;
F. Database Constraints
CREATE TABLE
statement, or after the table is created with the ALTER TABLE
statement.column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
The following constraints are commonly used in SQL:
NOT NULL
- Ensures that a column cannot have a NULL valueUNIQUE
- Ensures that all values in a column are differentPRIMARY KEY
- A combination of aNOT NULL
andUNIQUE
. Uniquely identifies each row in a table
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
FOREIGN KEY
- Prevents actions that would destroy links between tables
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
CHECK
- Ensures that the values in a column satisfies a specific condition
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);
DEFAULT
- Sets a default value for a column if no value is specified
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Sandnes'
);
CREATE INDEX
- Used to create and retrieve data from the database very quickly
G. Categories of Keys - Database management
H. Joining in Database
1. Inner joinINNER JOIN
keyword selects records that have matching values in both tables.FROM Products
INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID;
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.FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
3. Right outer join / Right Join
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.FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
The FULL OUTER JOIN
keyword returns all records when there is a match in left (table1) or right (table2) table records.
Tip: FULL OUTER JOIN
and FULL JOIN
are the same.
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
5. Cross Join
A self join is a regular join, but the table is joined with itself.
FROM table1 T1, table1 T2
WHERE condition;
I. Database View -imp
- Why we use view – to store complex query
Select * from student where roll = 101 and city = “Nagpur” and Percent > 60 and cast = “obc” or total >250 and total < 350 and sem = 1 and branch = “cse” and session = “23-24” and objective =”A” and section = “D”;
View name = report
- View Is called virtual table
- View store only for query result
- View is directly communicate to entity
There are following operation perform in a view
1. Create view
2. Update view
3. Delete view
View operator – As
Syntax :
Create view myview AS Select * from Demo2;
How to display view
select * from myview;
select * from myview where id=2;
How to update table
Syntax:
Alter view viewname As query
Ex.
Alter view my1 As Select * from Demo
Delete view
Syntax :
Drop view viewname;
Ex.
Drop view myview;
J. Store Procedure - imp
How to Create Procedure
Syntax :
Create Procedure procedurename
AS
BEGIN
//create all logic
END;
There are two method of procedure
1. Create procedure
2. Alter procedure
How to Create Procedure
Steps : Store procedure folder in server Explorer > right click > Add new Store Procedure
Sql server-
Database > program ability > store procedure
AS
BEGIN
SELECT * FROM Demo3;
END;
How to Execute Procedure
EXEC procedurename;
EXECUTE procedurename;
EXEC mypro;
Categories of Procedure
1. Non Parameterized Procedure
2. Parameterized Procedure
Parameterized Procedure
- All types of parameter inside the procedure is use to @ operator
EX.
CREATE PROCEDURE mypro2 (@name1 AS VARCHAR(50))
AS
BEGIN
SELECT * FROM student WHERE name = @name1;
END;
CREATE PROCEDURE mypro2(@name1 AS VARCHAR(50))
AS
BEGIN
Select * from join_inventry where name = @name1;
END;
How to pass parameter in a procedure
Syntax :
EXEC procedurename Parametervalue;
Ex.
EXEC mypro2 ‘tv’;
K. (DCL) Data Control Language
DCL deals with the access and permissions to the data within the database. It is used to control who can access the database, perform operations, and make changes to data and structures.
GRANT
: Provides specific privileges to a user or role.REVOKE
: Removes specific privileges from a user or role.
Interview Questions
When comparing UNION
vs. UNION ALL
, there is one major difference:
UNION
only returns uniqueUNION ALL
returns all records, including duplicates.
UNION
combines the result set of two or more SELECT
statements, showing only distinct values.
The SQL syntax below shows a UNION
occurring between two different tables; the columns in both SELECT
statements are of the same or matching data types.
The WHERE
clause shown is an optional inclusion:
SELECT
column_1, column_2
FROM
table_1
[
WHERE
condition]
UNION
SELECT
column_1, column_2
FROM
table_2
[
WHERE
condition]
Remember, UNION ALL
combines the results of two or more SELECT
statements, showing all values, including duplicates if they exist.
The SQL syntax below shows a UNION ALL
occurring between two different tables. Once again, the columns in the SELECT
are of the same or matching data types and the WHERE
clause is optional:
SELECT
column_1, column_2
FROM
table_1
[
WHERE
condition]
UNION
ALL
SELECT
column_1, column_2
FROM
table_2
[
WHERE
condition]
Comments
Post a Comment