⚡0 XP🔥0

Ch 9 · Structured Query Language (SQL) | SQL

Lesson 9 of 10 ⏱ 3 min read ✎ 1 exercises ❓ 5-question quiz ▶ Runs SQL

DDL and DML commands, data types, constraints, SELECT with WHERE, IN, BETWEEN, LIKE, IS NULL, ORDER BY, aggregate functions, GROUP BY and HAVING, and joins - with runnable examples.

Command categories#

  • DDL (structure): CREATE, ALTER, DROP.
  • DML (data): INSERT, UPDATE, DELETE, SELECT.
  • Data types: CHAR(n) (fixed), VARCHAR(n) (variable), INT, FLOAT, DATE.
  • Constraints: PRIMARY KEY, NOT NULL, UNIQUE, DEFAULT, FOREIGN KEY.
हिंदी में समझें

DDL कमांड तालिका की संरचना बनाते या बदलते हैं (CREATE, ALTER, DROP) और DML कमांड डेटा पर काम करते हैं (INSERT, UPDATE, DELETE, SELECT)। CHAR निश्चित लंबाई और VARCHAR परिवर्ती लंबाई का होता है।

SELECT and filtering#

Practise on the shop database (customers, products, orders, order_items, employees):

▶ Try it Yourselfdatabase: shopSQL
Ctrl + Enter to run
  • BETWEEN 100 AND 500 (inclusive), LIKE 'A%' (starts with A), LIKE '_a%' (second letter a), IS NULL, DISTINCT.

Aggregate functions, GROUP BY, HAVING#

COUNT(*), SUM(), AVG(), MAX(), MIN(). WHERE filters rows before grouping; HAVING filters groups.

▶ Try it Yourselfdatabase: shopSQL
Ctrl + Enter to run

Joins#

An equi-join combines rows of two tables with matching values; a natural join does the same on common column names without repeating them.

▶ Try it Yourselfdatabase: shopSQL
Ctrl + Enter to run

Changing data and structure#

UPDATE employees SET salary = salary * 1.10 WHERE dept = 'Sales';
DELETE FROM orders WHERE status = 'Cancelled';
ALTER TABLE customers ADD phone VARCHAR(15);
ALTER TABLE customers DROP phone;
DROP TABLE temp;            -- removes the whole table
हिंदी में समझें

WHERE समूह बनाने से पहले पंक्तियों को छाँटता है और HAVING समूह बनने के बाद समूहों को। DELETE केवल पंक्तियाँ हटाता है जबकि DROP पूरी तालिका को ही हटा देता है।

Exam tips#

  • COUNT(column) ignores NULLs; COUNT(*) counts all rows.
  • Difference between DELETE and DROP, WHERE and HAVING, CHAR and VARCHAR - frequent 2-mark questions.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Customers per city

Show each city and the number of customers in it as total, only for cities with at least 3 customers, sorted by total (highest first), then city name.

Database: shop (see the tables in this lesson)

Quick Quiz

Q1.

Which is a DDL command?

Q2.

To filter groups after GROUP BY use

Q3.

LIKE 'A%' matches names that

Q4.

COUNT(*) counts

Q5.

Command that removes the whole table is