0% found this document useful (0 votes)
29 views

SQL

This document discusses various SQL queries including selecting data with ordering, limiting, joining tables, aggregation, updating records, and using indexes and constraints. It provides examples of selecting data and ordering by country or multiple columns, limiting results, performing inner, left, right and full outer joins between tables, counting, averaging, summing data, creating clustered and non-clustered indexes, and defining constraints like NOT NULL, UNIQUE, PRIMARY KEY and FOREIGN KEY.

Uploaded by

Sudeep Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

SQL

This document discusses various SQL queries including selecting data with ordering, limiting, joining tables, aggregation, updating records, and using indexes and constraints. It provides examples of selecting data and ordering by country or multiple columns, limiting results, performing inner, left, right and full outer joins between tables, counting, averaging, summing data, creating clustered and non-clustered indexes, and defining constraints like NOT NULL, UNIQUE, PRIMARY KEY and FOREIGN KEY.

Uploaded by

Sudeep Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL

1. SELECT * FROM Customers


ORDER BY Country;

2. SELECT * FROM Customers


ORDER BY Country DESC;

3. SELECT * FROM Customers


ORDER BY Country, CustomerName;

4. SELECT * FROM Customers


ORDER BY Country ASC, CustomerName DESC;

5. SELECT * FROM artists LIMIT [Number to Limit By];


SELECT * FROM artists LIMIT 6;

6. SELECT * FROM artists LIMIT 5 OFFSET [Number of rows to skip];


SELECT * FROM artists LIMIT 5 OFFSET 2;

7. SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate


FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;

8. SELECT Orders.OrderID, Customers.CustomerName


FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

9. SELECT Customers.CustomerName, Orders.OrderID


FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;

10. SELECT Orders.OrderID, Employees.LastName, Employees.FirstName


FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;

11. SELECT Customers.CustomerName, Orders.OrderID


FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
SQL
12. SELECT COUNT(CustomerID), Country
13. FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;

14. SELECT MIN(Price) AS SmallestPrice


FROM Products;

15. SELECT MAX(Price) AS LargestPrice


FROM Products;

16. UPDATE Customers


SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;

17. SELECT COUNT(ProductID)


FROM Products;

18. SELECT AVG(Price)


FROM Products;

19. SELECT SUM(Quantity)


FROM OrderDetails;

20. Why Indexing is used?


Indexes are used to retrieve data from the database more quickly than otherwise. The users
cannot see the indexes, they are just used to speed up searches/queries.
CREATE INDEX idx_pname
ON Persons (LastName, FirstName);

21. Types of Indexing


Clustered Indexes
Clustered indexes are the unique index per table that uses the primary key to organize the data
that is within the table. The clustered index ensures that the primary key is stored in increasing
order, which is also the order the table holds in memory.

Clustered indexes do not have to be explicitly declared.


Created when the table is created.
Use the primary key sorted in ascending order.
SQL
Non-Clustered Indexes
Non-clustered indexes are sorted references for a specific field, from the main table, that hold
pointers back to the original entries of the table.

22. When to use Indexes


Indexes are meant to speed up the performance of a database, so use indexing whenever it
significantly improves the performance of your database. As your database becomes larger and
larger, the more likely you are to see benefits from indexing.

Testing Index performance


To test if indexes will begin to decrease query times, you can run a set of queries on your
database, record the time it takes those queries to finish, and then begin creating indexes and
rerunning your tests.

To do this, try using the EXPLAIN ANALYZE clause in PostgreSQL.:

EXPLAIN ANALYZE SELECT * FROM friends WHERE name = 'Blake';

23. SQL Constraints


SQL constraints are used to specify rules for the data in a table.

Constraints are used to limit the type of data that can go into a table. This ensures the accuracy
and reliability of the data in the table. If there is any violation between the constraint and the
data action, the action is aborted.

Constraints can be column level or table level. Column level constraints apply to a column, and
table level constraints apply to the whole table.

The following constraints are commonly used in SQL:

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


UNIQUE - Ensures that all values in a column are different
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
CHECK - Ensures that the values in a column satisfies a specific condition
DEFAULT - Sets a default value for a column if no value is specified
CREATE INDEX - Used to create and retrieve data from the database very quickly

You might also like