Sunday, January 3, 2021

SQL Queries Tutorial | Sql Query tutorial for Beginners with Examples

This is my first post of the year 😀. As I mentioned in my SQL Queries Tutorial and Sql Query tutorial for beginners with examples, the SQL queries that you can use for practice are below.
 
SQL Queries (these SQL queries in DBMS are explained in the above SQL Tutorial for Beginners):
 
1) [SQL for beginners] Get all fields of all records from the Customers table.
select * from Customers
2) [SQL for beginners] Select only the named fields from the Customers table.
select CustomerID, CustomerName, Country from Customers
3) [SQL for beginners] Select only 5 records and only the given fields from the Customers table.
select top 5 CustomerID, CustomerName, Country from Customers
4) [SQL for beginners] Select only the given fields from all records which match the given condition in the Customers table.
select  CustomerID, CustomerName, Country from Customers where Country = 'USA'
5) [SQL for beginners] Select all fields of all records from the Employees table.
select * from Employees
6) Select all records from the Employees table ordered by FirstName field.
select * from Employees order by FirstName
7) Select all records from the Employees table ordered by FirstName field in descending order.
select * from Employees order by FirstName desc
8) Select OrderID field with alias ID, CustomerID field with alias Customer and so on from the Orders table.
select OrderID as ID, CustomerID as Customer, OrderDate as [Date] from Orders
9) [SQL Joins] Select with Inner Join of Orders table with Customers table.
select Orders.OrderID, Customers.CustomerName, Customers.Country
from Orders inner join Customers on Orders.CustomerID = Customers.CustomerID
10) Select those Employees whose EmployeeID does not appear in the Orders table.
select * from Employees where EmployeeID not in (select EmployeeID from Orders)
11) [SQL Joins] Select with Left Join of Employees table with Orders table.
select Employees.FirstName, Employees.LastName, Orders.OrderID
from Employees left join Orders on Employees.EmployeeID =Orders.EmployeeID
order by Employees.FirstName, Employees.LastName
12) [SQL Joins] Select with Right Join of Orders table with Employees table.
select Employees.FirstName, Employees.LastName, Orders.OrderID
from Orders right join Employees
on Employees.EmployeeID =Orders.EmployeeID
order by Employees.FirstName, Employees.LastName
13) [SQL Joins] Select pairs of products with the same price using Self Join of Products table.
select P1.ProductID as ID1, P1.ProductName as Name1, P1.Price as Price1, P2.ProductID as ID2, P2.ProductName as Name2, P2.Price as Price2
from Products P1 inner join Products P2 on P1.Price = P2.Price and P1.ProductID <> P2.ProductID
14) [SQL queries examples] Using Union, select Country field values from the Customers table and Suppliers table.
select Country from Customers union select Country from Suppliers
15) [SQL queries examples] Using Union All, select Country field values including duplicates from the Customers table and Suppliers table.
select Country from Customers union all select Country from Suppliers
16) Using Group By, count the total number of suppliers per country from the Suppliers table.
select count(SupplierID) as TotalSuppliers, Country
from Suppliers group by Country
17) Using Group By, count the total number of products per price point from the Products table.
select count(ProductID) as ProductsNumber, Price
from Products group by Price order by Price
18) Using the above SQL query, select only those price points that have more than one product.
select count(ProductID) as ProductsNumber, Price
from Products group by Price having count(ProductID)>1 order by Price
19) Using a sub query, select those customers who have ordered any quantity more than 90 units.
select CustomerID, CustomerName from Customers 
where CustomerID = any (select Orders.CustomerID from Orders inner join OrderDetails on Orders.OrderID = OrderDetails.OrderID  where OrderDetails.Quantity > 90)

You can practice the above SQL Queries on W3Schools website.

No comments:

Post a Comment