SQL
SQL
SQL Exercises : SQL Practice with Solution for Beginners and Experienced | GeeksforGeeks
SQL Cheat Sheet ( Basic to Advanced) | GeeksforGeeks
SQL Interview Questions | GeeksforGeeks
SQL Query Interview Questions | GeeksforGeeks
RANK(): Assigns a rank to each row, with gaps if there are ties.
DENSE_RANK(): Assigns consecutive ranks without any gaps.
ROW_NUMBER(): Assigns a unique number to each row regardless of ties.
RANK(): Assigns the same number to tied rows and leaves gaps for
subsequent ranks.
SQL keywords are NOT case sensitive: select is the same as SELECT
Semicolon is the standard way to separate each SQL statement in
database systems that allow more than one SQL statement to be
executed in the same call to the server.
Single line comments start with --
Multi-line comments start with /* and end with */
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely
identifies each row in a table
FOREIGN KEY - Prevents actions that would destroy links between tables
SQL can:
Execute queries, retrieve data
Insert, update, delete records from a database
Create new databases
Create new tables in a database
Create stored procedures in a database
Create views in a database
Set permissions on tables, procedures, and views
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
The UNION operator selects only distinct values by default. To allow duplicate
values, use UNION ALL:
The EXISTS operator is used to test for the existence of any record in a
subquery.
The EXISTS operator returns TRUE if the subquery returns one or more
records.
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
SELECT ProductName
FROM Products
WHERE ProductID = ANY ALL
(SELECT ProductID
FROM OrderDetails
WHERE Quantity > 1000);
SELECT *
INTO temp IN 'Backup.mdb' IN clause copy table into a new table in another
database
FROM Customers;
Copy "table1" into "table2" (the columns that are not filled with data, will
contain NULL):
INSERT INTO table2 (CustomerName, City, Country)
SELECT SupplierName, City, Country FROM table1;
View is a virtual table, shows up-to-date data! The database engine recreates
the view, every time a user queries it.
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
DROP VIEW view_name;
Write a query to find the employees who joined in the last 30 days.
SELECT *
FROM Employee
WHERE JoiningDate > DATE_SUB(CURDATE(), INTERVAL 30 DAY);
Write a query to fetch employees whose names start and end with
‘A’.
SELECT *
FROM Employee
WHERE Name LIKE 'A%' AND Name LIKE '%A';