Posts

PostgreSQL Subqueries and CTEs: Practical SQL Guide for Beginners

Subqueries and Common Table Expressions (CTEs) are important PostgreSQL concepts used to write readable queries, filter data based on other query results, and solve real-world business problems. In this tutorial, you will learn how to use subqueries, correlated subqueries, and CTEs with practical SQL examples. Sample Table We will use an employees table with the following data: employee_id employee_name department salary 1 Rahul IT 60000 2 Priya HR 40000 3 Arjun IT 75000 4 Sneha Finance 50000 5 Kiran HR 45000 6 Divya Finance 65000 What Is a Subquery? A subquery is a SQL query written inside another SQL query. It is also called an inner query or nested query. Subqueries can be used inside SELECT , FROM , WHERE , and HAVING clauses. Example 1: Find Employees Earning More Than the Average Salary SELECT employee_name, salary FROM employees WHERE salary > ( SELECT AVG(salary) FROM employees ); The inner query calculates the average salary. The outer query returns employees whos...

PostgreSQL SELF JOIN, CROSS JOIN, UNION and UNION ALL: Practical SQL Guide

 These concepts are useful for SQL Developers, Data Analysts, and technical interviews. What You Will Learn What is a SELF JOIN? What is a CROSS JOIN? Difference between UNION and UNION ALL Practical SQL examples Common mistakes and interview questions 1. What Is a SELF JOIN? A SELF JOIN joins a table with itself. It is useful when records in the same table are related to each other. For example, an employee table may contain both employees and their managers. Create an Employee Table CREATE TABLE company_employees ( employee_id INT PRIMARY KEY, employee_name VARCHAR(50), manager_id INT ); INSERT INTO company_employees VALUES (1, 'Rahul', NULL), (2, 'Priya', 1), (3, 'Arjun', 1), (4, 'Sneha', 2), (5, 'Kiran', 2); Here, manager_id refers to another employee_id in the same table. Example: Display Employees and Their Managers SELECT e.employee_name AS employee, m.employee_name AS manager FROM company_employees e LEFT JOIN company...

PostgreSQL JOINs Explained: INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL OUTER JOIN

 PostgreSQL JOINs help you retrieve related data from two or more tables. They are essential for SQL developers, Data Analysts, and anyone working with relational databases. For example, a company may store employee information in one table and department information in another. JOINs allow you to combine these tables and generate meaningful reports. What You Will Learn What is a JOIN in PostgreSQL? INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN JOIN conditions and common mistakes Practical exercises and interview questions 1. Create Sample Tables We will use two tables: employees and departments. Departments table CREATE TABLE departments ( department_id INT PRIMARY KEY, department_name VARCHAR(50) ); INSERT INTO departments VALUES (1, 'Analytics'), (2, 'Finance'), (3, 'IT'), (4, 'HR'); Employees table CREATE TABLE employees ( employee_id INT PRIMARY KEY, employee_name VARCHAR(50), department_id INT ); INSERT INTO employees VALUES...

PostgreSQL Aggregate Functions, GROUP BY and HAVING: Practical SQL Guide

In this tutorial, you will learn how to use PostgreSQL aggregate functions, GROUP BY, and HAVING with practical SQL examples. These concepts are useful for database reporting, data analytics, and SQL interviews. What You Will Learn COUNT(), SUM(), AVG(), MIN(), and MAX() GROUP BY with practical examples WHERE vs HAVING Common SQL mistakes Practice questions and interview questions 1. What Are Aggregate Functions in PostgreSQL? Aggregate functions perform calculations on multiple rows and return a single result. They help calculate totals, averages, counts, minimum values, and maximum values. For example, a company may want to calculate its total salary expense or find the average salary of employees. 2. Create a Sample Table We will use an employees table for all examples in this tutorial. CREATE TABLE employees ( employee_id INT PRIMARY KEY, employee_name VARCHAR(50), department VARCHAR(30), salary NUMERIC(10,2) ); INSERT INTO employees (employee_id, employee_name, de...

SQL SELECT, WHERE and ORDER BY: A Practical Guide for Beginners

SQL SELECT, WHERE and ORDER BY: A Practical Guide for Beginners Introduction SQL is one of the most important skills for anyone who wants to become a Data Analyst, SQL Developer, or Database Developer. In this tutorial, we will learn how to retrieve, filter, and sort data using SELECT, WHERE, AND, OR, and ORDER BY clauses. All examples use PostgreSQL syntax. 1. What Is the SELECT Statement? The SELECT statement retrieves data from a database table. To retrieve all columns from the employees table, use: SELECT * FROM employees; The asterisk (*) represents all columns. To retrieve only employee names and salaries, use: SELECT employee_name, salary FROM employees; This query returns only the employee_name and salary columns. 2. What Is the WHERE Clause? The WHERE clause filters records based on a condition. To find employees whose salary is greater than 40000, use: SELECT * FROM employees WHERE salary > 40000; To find employees who work in the IT department, use: SELECT * FROM employee...

PostgreSQL Tutorial for Beginners: Learn SQL with Practical Examples

Introduction PostgreSQL is a powerful, open-source relational database management system used to store, organize, and retrieve data. It is widely used in web applications, business software, reporting systems, and data analytics projects. In this tutorial, you will learn the fundamentals of PostgreSQL through practical examples. You will understand how to create tables, insert records, retrieve data, filter results, and summarize information using SQL queries. This guide is designed for beginners who want to develop practical database skills for software development, data analytics, and other technology-related careers. What You Will Learn By the end of this tutorial, you will understand: What PostgreSQL is and how it works. How to create a database table. How to insert and retrieve records. How to filter and sort data. How to summarize data using aggregate functions. How to practice SQL using a business-related example. What Is PostgreSQL? PostgreSQL is an open-source relational datab...