January 03, 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.

17 comments:

  1. Started out in 2012, Data Science Central is one of the industry’s leading and fastest growing Internet
    community for data practitioners. Whether it is data science or machine learning or deep learning or
    big data, Data Science Central is a one-stop shop that covers a wide range of data science topics that
    includes technology, tools, data visualisation, code, and job opportunities. Also, many industry experts
    contribute to the community forum for discussion or questions.

    DATASCIENCETraining in OMR Chennai

    ReplyDelete

  2. The Blog is very nice , thank you for this blog! I get my video course form (Top In IELTS Video Courses
    ),The Top in IELTS video courses is designed for those students who due to some reasons cannot avail the facility of online and classroom training.

    ReplyDelete
  3. Well written articles like yours renews my faith in today's writers. The article is very informative. Thanks for sharing such beautiful information.
    Best Data Migration tools
    Penetration testing companies USA
    What is Data Lake
    Artificial Intelligence in Banking
    What is Data analytics
    Big data Companies USA

    ReplyDelete
  4. Thanks for uploading this post its really kind of content that i looking for very helpful post by the away. If anyone looking for best Software training institute in Delhi Contact Here-+91-9311002620 Or Visit our website https://www.htsindia.com/software-testing-training-courses

    ReplyDelete
  5. This information is impressive; I am inspired how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic snowflake data warehouse

    ReplyDelete
  6. Thank You for sharing this blog.
    When you choose software development exclusive to your company needs, you are inviting in innovation, streamlined work processes, better communication between departments, quicker turnaround times, more efficiency, and productivity.
    Hire Software Developer from Yazyk for better growth of your business.

    ReplyDelete
  7. Thank You for this wonderful and much required information in this post.
    Application Modernization Services in USA

    ReplyDelete
  8. Thanks for sharing this informative post and very knowledgeable content you put on that by the way. If anyone looking for best Software training institute in Delhi Contact Here-+91-9311002620 Or Visit our website https://www.htsindia.com/software-testing-training-courses

    ReplyDelete
  9. Very nice post thank you for sharing this post its very knowledgeable and very helpful i hope that you will continue to post these kinds of contents in future apart from that if anyone looking for Advance Excel institute in delhi so Contact Here-+91-9311002620 Or Visit Website- https://www.htsindia.com/Courses/business-analytics/adv-excel-training-course

    ReplyDelete
  10. Hey,
    Thanks for sharing this great blog. It contains a lot of information. It is easy to locate a Digital E-Learning Service in India. But hard to choose the best Web Design services like this. All your services look very professional. Keep posting.

    ReplyDelete

Note: Only a member of this blog may post a comment.