SQL

Writing efficient SQL queries, database design, window functions, and advanced techniques across PostgreSQL, MySQL, and SQLite.

SQL

SQL - Window Functions Complete Guide

Window functions operate on a set of rows and return a single value for each row, unlike aggregate functions that collapse multiple rows into one. They’re called ‘window’ functions because they…

Read more →
SQL

SQL - UPDATE Statement

The UPDATE statement modifies existing records in a table. The fundamental syntax requires specifying the table name, columns to update with their new values, and a WHERE clause to identify which…

Read more →
SQL

SQL - UPPER() and LOWER()

UPPER() converts all characters in a string to uppercase, while LOWER() converts them to lowercase. Both functions accept a single string argument and return the transformed result.

Read more →
SQL

SQL - User-Defined Functions (UDF)

SQL Server supports three primary UDF types: scalar functions, inline table-valued functions (iTVF), and multi-statement table-valued functions (mTVF). Each type has specific performance…

Read more →
SQL

SQL - Triggers with Examples

Triggers execute automatically in response to data modification events. Unlike stored procedures that require explicit invocation, triggers fire implicitly when specific DML operations occur. This…

Read more →
SQL

SQL - TRUNCATE vs DELETE vs DROP

SQL provides three distinct commands for removing data: TRUNCATE, DELETE, and DROP. Each serves different purposes and has unique characteristics that impact performance, recoverability, and side…

Read more →
SQL

SQL - UNIQUE Constraint

• UNIQUE constraints prevent duplicate values in columns while allowing NULL values (unlike PRIMARY KEY), making them essential for enforcing business rules on alternate keys like email addresses,…

Read more →
SQL

SQL - Temporary Tables

Temporary tables are database objects that store intermediate result sets during query execution. Unlike permanent tables, they exist only for the duration of a session or transaction and are…

Read more →
SQL

SQL - Stored Procedures Tutorial

Stored procedures are precompiled SQL statements stored in the database that execute as a single unit. Unlike ad-hoc queries sent from applications, stored procedures reside on the database server…

Read more →
SQL

SQL - STUFF() / INSERT()

• SQL Server’s STUFF() and MySQL’s INSERT() perform similar string manipulation by replacing portions of text at specified positions, but with different syntax and parameter ordering

Read more →
SQL

SQL - REVERSE() Function

• The REVERSE() function inverts character order in strings, useful for palindrome detection, data validation, and specialized sorting operations

Read more →
SQL

SQL - ROW_NUMBER() Function

ROW_NUMBER() is a window function that assigns a unique sequential integer to each row within a partition of a result set. The numbering starts at 1 and increments by 1 for each row, regardless of…

Read more →
SQL

SQL - PIVOT and UNPIVOT

PIVOT transforms rows into columns by rotating data around a pivot point. The operation requires three components: an aggregate function, a column to aggregate, and a column whose values become new…

Read more →
SQL

SQL - PRIMARY KEY Constraint

• PRIMARY KEY constraints enforce uniqueness and non-null values on one or more columns, serving as the fundamental mechanism for row identification in relational databases

Read more →
SQL

SQL - Query Execution Plan Explained

• Query execution plans reveal how the database engine processes your SQL statements, showing the actual operations, join methods, and data access patterns that determine query performance

Read more →
SQL

SQL - RANK() Function

The RANK() function assigns a rank to each row within a result set partition. When two or more rows have identical values in the ORDER BY columns, they receive the same rank, and subsequent ranks…

Read more →
SQL

SQL - REPEAT() / REPLICATE()

• REPEAT() (MySQL/PostgreSQL) and REPLICATE() (SQL Server/Azure SQL) generate strings by repeating a base string a specified number of times, useful for formatting, padding, and generating test data

Read more →
SQL

SQL - NTILE() Function

NTILE() is a window function that distributes rows into a specified number of ordered groups. Each row receives a bucket number from 1 to N, where N is the number of groups you define.

Read more →
SQL

SQL - NULLIF() Function

NULLIF() accepts two arguments and compares them for equality. If the arguments are equal, it returns NULL. If they differ, it returns the first argument. The syntax is straightforward:

Read more →
SQL

SQL - ORDER BY in Window Functions

Window functions operate on a ‘window’ of rows related to the current row. The ORDER BY clause within the OVER() specification determines how rows are ordered within each partition for the window…

Read more →
SQL

SQL - PARTITION BY Clause

The PARTITION BY clause defines logical boundaries within a result set for window functions. Unlike GROUP BY, which collapses rows into aggregate summaries, PARTITION BY maintains all original rows…

Read more →
SQL

SQL - Partitioning Tables

• Table partitioning divides large tables into smaller physical segments while maintaining a single logical table, dramatically improving query performance by enabling partition pruning where the…

Read more →
SQL

SQL - PERCENT_RANK() and CUME_DIST()

PERCENT_RANK() calculates the relative rank of each row within a result set as a percentage. The formula is: (rank - 1) / (total rows - 1). This means the first row always gets 0, the last row gets…

Read more →
SQL

SQL - Materialized Views

A materialized view is a database object that stores the result of a query physically on disk. Unlike regular views that execute the underlying query each time they’re accessed, materialized views…

Read more →
SQL

SQL - MERGE / UPSERT Statement

MERGE statements solve a common data synchronization problem: you need to insert a row if it doesn’t exist, or update it if it does. The naive approach—checking existence with SELECT, then branching…

Read more →
SQL

SQL - NOT NULL Constraint

The NOT NULL constraint ensures a column cannot contain NULL values. Unlike other constraints that validate relationships or value ranges, NOT NULL addresses the fundamental question: must this field…

Read more →
SQL

SQL - LEFT() and RIGHT()

The LEFT() and RIGHT() functions extract substrings from text fields. LEFT() starts from the beginning, RIGHT() from the end. Both accept two parameters: the string and the number of characters to…

Read more →
SQL

SQL - LIKE Operator and Wildcards

The LIKE operator compares a column value against a pattern containing wildcard characters. The two standard wildcards are % (matches any sequence of characters) and _ (matches exactly one…

Read more →
SQL

SQL - LIMIT / TOP / FETCH FIRST

• LIMIT, TOP, and FETCH FIRST are database-specific syntaxes for restricting query result sets, with FETCH FIRST being the SQL standard approach supported by modern databases

Read more →
SQL

SQL - LPAD() and RPAD()

LPAD() and RPAD() are string manipulation functions that pad a string to a specified length by adding characters to the left (LPAD) or right (RPAD) side. The syntax is consistent across most SQL…

Read more →
SQL

SQL - IS NULL / IS NOT NULL

NULL is a special marker in SQL that indicates missing, unknown, or inapplicable data. Unlike empty strings (’’) or zeros (0), NULL represents the absence of any value. This distinction matters…

Read more →
SQL

SQL - JSON Functions in SQL

Most modern relational databases support native JSON data types that validate and optimize JSON storage. PostgreSQL, MySQL 8.0+, SQL Server 2016+, and Oracle 12c+ all provide JSON capabilities with…

Read more →
SQL

SQL - Lateral Join / CROSS APPLY

• Lateral joins (PostgreSQL) and CROSS APPLY (SQL Server) enable correlated subqueries in the FROM clause, allowing each row from the left table to pass parameters to the right-side table expression

Read more →
SQL

SQL - LEAD() and LAG() Functions

LEAD() and LAG() belong to the window function family, operating on a ‘window’ of rows related to the current row. Unlike aggregate functions that collapse multiple rows into one, window functions…

Read more →
SQL

SQL - INSERT INTO Statement

• The INSERT INTO statement adds new rows to database tables using either explicit column lists or positional values, with explicit lists being safer and more maintainable in production code.

Read more →
SQL

SQL - INTERSECT and EXCEPT/MINUS

Set operations treat query results as mathematical sets, allowing you to combine, compare, and filter data from multiple SELECT statements. While JOIN operations combine columns from different…

Read more →
SQL

SQL - IN Operator with Examples

The IN operator tests whether a value matches any value in a specified list or subquery result. It returns TRUE if the value exists in the set, FALSE otherwise, and NULL if comparing against NULL…

Read more →
SQL

SQL - Error Handling (TRY...CATCH)

SQL Server’s TRY…CATCH construct wraps potentially error-prone code in a TRY block, transferring control to the CATCH block when errors occur. This prevents automatic termination and allows…

Read more →
SQL

SQL - FOREIGN KEY Constraint

A foreign key constraint establishes a link between two tables by ensuring that values in one table’s column(s) match values in another table’s primary key or unique constraint. This relationship…

Read more →
SQL

SQL - DEFAULT Constraint

• DEFAULT constraints provide automatic fallback values when INSERT or UPDATE statements omit column values, reducing application-side logic and ensuring data consistency

Read more →
SQL

SQL - DELETE Statement

The DELETE statement removes one or more rows from a table. The fundamental syntax requires only the table name, but production code should always include a WHERE clause to avoid catastrophic data…

Read more →
SQL

SQL - DENSE_RANK() Function

DENSE_RANK() is a window function that assigns a rank to each row within a partition of a result set. The key characteristic that distinguishes it from other ranking functions is its handling of…

Read more →
SQL

SQL - DROP TABLE

The DROP TABLE statement removes a table definition and all associated data, indexes, triggers, constraints, and permissions from the database. Unlike TRUNCATE, which removes only data, DROP TABLE…

Read more →
SQL

SQL - Dynamic SQL with Examples

Dynamic SQL refers to SQL statements that are constructed and executed at runtime rather than being hard-coded in your application. This approach becomes necessary when query structure depends on…

Read more →
SQL

SQL - Cursors Tutorial

Cursors provide a mechanism to traverse result sets one row at a time, enabling procedural logic within SQL Server. While SQL excels at set-based operations, certain scenarios require iterative…

Read more →
SQL

SQL - COUNT() as Window Function

• COUNT() as a window function calculates running totals and relative frequencies without collapsing rows, unlike its aggregate counterpart which groups results into single rows per partition

Read more →
SQL

SQL - CREATE INDEX and DROP INDEX

Indexes function as lookup tables that map column values to physical row locations. Without an index, the database performs a full table scan, examining every row sequentially. With a proper index,…

Read more →
SQL

SQL - CREATE TABLE Statement

• The CREATE TABLE statement defines both the table structure and data integrity rules through column definitions, data types, and constraints that enforce business logic at the database level

Read more →
SQL

SQL - CREATE VIEW with Examples

• Views act as virtual tables that store SQL queries rather than data, providing abstraction layers that simplify complex queries and enhance security by restricting direct table access

Read more →
SQL

SQL - Complete Tutorial for Beginners

SQL (Structured Query Language) is the standard language for interacting with relational databases. Unlike procedural programming languages, SQL is declarative—you describe the result you want, and…

Read more →
SQL

SQL - CAST() and CONVERT() Functions

Type conversion transforms data from one data type to another. SQL handles this through implicit (automatic) and explicit (manual) conversion. Implicit conversion works when SQL Server can safely…

Read more →
SQL

SQL - CHECK Constraint

CHECK constraints define business rules directly in the database schema by specifying conditions that column values must satisfy. Unlike foreign key constraints that reference other tables, CHECK…

Read more →
SQL

SQL - AND, OR, NOT Operators

Logical operators form the backbone of conditional filtering in SQL queries. These operators—AND, OR, and NOT—allow you to construct complex WHERE clauses that precisely target the data you need….

Read more →
SQL

Spark SQL - Window Functions Tutorial

Window functions perform calculations across a set of rows that are related to the current row. Unlike aggregate functions with GROUP BY that collapse multiple rows into one, window functions…

Read more →
SQL

Spark SQL - Hive Integration

To enable Hive support in Spark, you need the Hive dependencies and proper configuration. First, ensure your spark-defaults.conf or application code includes Hive metastore connection details:

Read more →
SQL

Spark SQL - JSON Functions

• Spark SQL provides over 20 specialized JSON functions for parsing, extracting, and manipulating JSON data directly within DataFrames without requiring external libraries or UDFs

Read more →
SQL

Spark SQL - Managed vs External Tables

Spark SQL supports two table types that differ in how they manage data lifecycle and storage. Managed tables (also called internal tables) give Spark full control over both metadata and data files….

Read more →
SQL

Spark SQL - Map Functions

• Map functions in Spark SQL enable manipulation of key-value pair structures through native SQL syntax, eliminating the need for complex UDFs or RDD operations in most scenarios

Read more →
SQL

Spark SQL - Struct Type Operations

Struct types represent complex data structures within a single column, similar to objects in programming languages or nested JSON documents. Unlike primitive types, structs contain multiple named…

Read more →
SQL

Spark SQL - Aggregate Functions

Spark SQL provides comprehensive aggregate functions that operate on grouped data. The fundamental pattern involves grouping rows by one or more columns and applying aggregate functions to compute…

Read more →
SQL

Spark SQL - Array Functions

• Spark SQL provides 50+ array functions that enable complex data transformations without UDFs, significantly improving performance through Catalyst optimizer integration and whole-stage code…

Read more →
SQL

Spark SQL - Catalog API

The Spark Catalog API exposes metadata operations through the SparkSession.catalog object. This interface abstracts the underlying metastore implementation, whether you’re using Hive, Glue, or…

Read more →
SQL

Spark SQL - Create Database and Tables

Spark SQL databases are logical namespaces that organize tables and views. By default, Spark creates a default database, but production applications require proper database organization for better…

Read more →
SQL

Spark SQL - Data Types Reference

• Spark SQL supports 20+ data types organized into numeric, string, binary, boolean, datetime, and complex categories, with specific handling for nullable values and schema evolution

Read more →