Is SQL a programming language or a scripting language?

SQL (Structured Query Language) is a domain-specific programming language, but not in the same sense as general-purpose programming languages like Python, Java, or C#.

Here’s the breakdown:

  • Programming Language.
    • SQL is standardized (ANSI/ISO) and has defined syntax and semantics.
    • It can express logic (queries, conditions, joins, transactions).
    • It’s declarative — you describe what you want (e.g., “SELECT all users where age > 30”) rather than how to do it.
  • Not typically called a scripting language.
    • Scripting languages (like Python, JavaScript, Bash) are usually procedural, telling the computer step-by-step what to do.
    • SQL doesn’t work this way—it relies on the database engine to figure out the execution plan.

So, SQL is generally considered a declarative programming language, not a scripting language.

That said, some SQL dialects (like PL/SQL for Oracle or T-SQL for SQL Server) add procedural and scripting-like features (loops, conditionals, error handling), which make them feel closer to scripting.

Can you compare pure SQL vs PL/SQL/T-SQL scripting so that I can see the difference?

Pure SQL (declarative)

Say we want to find all employees who earn more than $50,000:

SELECT first_name, last_name, salary

FROM Employees

WHERE salary > 50000;

  • This tells the database engine what we want.
  • The DBMS figures out how to get it (execution plan, indexes, etc.).
  • No loops, no procedural logic—just a query.

PL/SQL (Oracle) or T-SQL (SQL Server) (procedural + scripting features)

Example: Give employees a 10% raise if they earn less than $50,000.

T-SQL (SQL Server):

BEGIN

   UPDATE Employees

   SET salary = salary * 1.10

   WHERE salary < 50000;

   PRINT ‘Salaries updated successfully’;

END

PL/SQL (Oracle):

BEGIN

   FOR emp IN (SELECT employee_id, salary FROM Employees WHERE salary < 50000) LOOP

      UPDATE Employees

      SET salary = emp.salary * 1.10

      WHERE employee_id = emp.employee_id;

   END LOOP;

   DBMS_OUTPUT.PUT_LINE(‘Salaries updated successfully’);

END;

  • Here you see variables, loops, conditionals, and procedural logic.
  • This is scripting-like behavior wrapped around SQL statements.

Summary:

  • SQL → Declarative, describes what data you want.
  • PL/SQL / T-SQL → Procedural, scripting-style extensions to SQL for control flow and business logic.

Can you also compare SQL with a true scripting language (like Python) running SQL queries, so that I can see how they complement each other?

Let’s line them up so you can see the contrast clearly.

1. Pure SQL

 (declarative inside the database)

SELECT first_name, last_name

FROM Employees

WHERE salary > 50000;

  • Runs inside the database.
  • Asks what you want, and the database figures out how to retrieve it.
  • No flow control (loops, conditions, etc.).

2. SQL with Procedural Extensions (T-SQL / PL/SQL)

BEGIN

   DECLARE @count INT;

   SELECT @count = COUNT(*) FROM Employees WHERE salary > 50000;

   IF @count > 100

      PRINT ‘High number of well-paid employees!’;

   ELSE

      PRINT ‘Normal salary distribution.’;

END;

  • Adds variables, conditionals, loops, error handling.
  • Still runs inside the database engine.
  • Feels like scripting inside SQL.

3. Scripting Language (Python) using SQL

import sqlite3

# Connect to database

conn = sqlite3.connect(“company.db”)

cursor = conn.cursor()

# Run a SQL query

cursor.execute(“SELECT first_name, last_name FROM Employees WHERE salary > 50000”)

# Work with results in Python

for row in cursor.fetchall():

    print(f”Employee: {row[0]} {row[1]}”)

conn.close()

  • SQL is embedded inside Python.
  • Python handles loops, printing, business logic, API calls, etc.
  • SQL only handles data retrieval/manipulation.

 In summary:

  • SQL = a declarative programming language (data only).

  • PL/SQL / T-SQL = SQL + scripting features inside the database.
  • Python / JavaScript / Bash = general-purpose scripting languages that can call SQL.

Here is a diagram that shows how these three layers (SQL, procedural SQL, and external scripting) interact with the database?

Here’s a diagram that shows how SQL, procedural SQL (PL/SQL / T-SQL), and external scripting languages (like Python/JS) all interact with the database engine.

error: Content is protected !!