Class 12 · Computer Science · Computer Science
Chapter 9: Structured Query Language (SQL) Notes
The big idea
SQL is the standard language to define, fill, change and query relational databases like MySQL: DDL builds tables, DML changes data, SELECT retrieves it, and functions, GROUP BY and joins answer richer questions.
Things to remember
- SQL statements are not case sensitive, end with a semicolon and say what to fetch, not how. MySQL data types: CHAR(n) fixed length, VARCHAR(n) variable length, INT, FLOAT and DATE ('YYYY-MM-DD'). Constraints: NOT NULL, UNIQUE, DEFAULT, PRIMARY KEY, FOREIGN KEY.
- DDL (Data Definition Language): CREATE DATABASE, USE, SHOW TABLES, CREATE TABLE name(column datatype constraint, ...), DESCRIBE and DROP TABLE or DATABASE (permanent). ALTER TABLE can ADD or DROP a column or primary key, ADD FOREIGN KEY, ADD UNIQUE, and MODIFY a datatype, NOT NULL or DEFAULT.
- DML (Data Manipulation Language): INSERT INTO table VALUES (...) adds a row, with text and dates in single quotes; UPDATE table SET column = value WHERE condition changes rows; DELETE FROM table WHERE condition removes rows. Without WHERE, UPDATE and DELETE affect every row.
- SELECT columns FROM table WHERE condition retrieves data; the star means all columns. AS renames a column in output, and DISTINCT removes duplicates. Conditions use =, !=, comparison operators, AND, OR, NOT, BETWEEN (inclusive), IN and NOT IN.
- ORDER BY sorts ascending by default and descending with DESC. NULL means unknown and is tested only with IS NULL or IS NOT NULL, never with an equals sign. LIKE does pattern matching: percent stands for any number of characters and underscore for exactly one, so LIKE 'K%' finds names beginning with K.
- Single-row functions return one result per row: POWER(2,3) is 8, ROUND(2912.564,1) is 2912.6, MOD(21,2) is 1; UCASE, LCASE, LENGTH, LEFT, RIGHT, MID('Informatics',3,4) is form, INSTR('Informatics','ma') is 6, TRIM; and date functions NOW, YEAR, MONTH, MONTHNAME, DAY, DAYNAME.
- Aggregate (multiple-row) functions give one result for a group: MAX, MIN, AVG, SUM, COUNT(*) which counts all rows and COUNT(column) which ignores NULLs. GROUP BY groups rows with equal values, and HAVING filters the groups, while WHERE filters individual rows before grouping.
- UNION, INTERSECT and MINUS need tables with the same number of attributes and matching domains. Cartesian product pairs every row with every row: degree adds and cardinality multiplies (4 and 5 rows give 20). JOIN with ON, or NATURAL JOIN, matches rows on a common attribute; N tables need N minus 1 joins.
Words to know
Tap a card to see what it means
Quick quiz
⭐ 0 / 5In EMPLOYEE (10 rows), Bonus is NULL for three employees. What do SELECT COUNT(*) and SELECT COUNT(Bonus) return?
A table of 4 rows and 3 columns is combined with a table of 5 rows and 3 columns using SELECT * FROM A, B. What are the degree and cardinality of the result?
SALE has customers C0001 (2 sales), C0002 (2 sales), C0003 (1 sale) and C0004 (1 sale). What does SELECT CustID, COUNT(*) FROM SALE GROUP BY CustID HAVING COUNT(*) greater than 1 display?
Which query correctly lists employees whose name has the letter a as its second character?
Assertion (A): WHERE Bonus = NULL does not return employees with no bonus. Reason (R): NULL stands for an unknown value, so it must be tested with IS NULL. Choose the correct option.
Match the pairs
Match each SQL item with its job.
Try this at home
Draw a table of your class friends with columns for roll number, name and city. Write down on paper the SELECT statements you would use to list all names, only friends from one city, and the names in alphabetical order.
All Computer Science notesChapter page and exercisesComputer Science syllabus