What is SQL?

The standard language for defining tables, searching, inserting, updating and deleting data, and controlling privileges in relational databases. It is standardized by ISO/IEC. Its statements include SELECT, INSERT, UPDATE, DELETE, CREATE TABLE and GRANT.

Japanese name: SQL

SQL appears in 15 practice questions across 3 topics, including Database Basics, Information Security Basics, IC design flow and EDA.

Field: Database Basics

Kinds of SQL statements

SQL statements fall into three broad groups by role. The data definition language (DDL), such as CREATE, ALTER and DROP, defines structures like tables, views and indexes. The data manipulation language (DML), such as SELECT, INSERT, UPDATE and DELETE, handles the data in tables. The data control language (DCL), such as GRANT and REVOKE, controls privileges. COMMIT, which makes a transaction permanent, and ROLLBACK, which cancels it, are sometimes counted as DCL and sometimes treated separately as transaction control statements.

Clauses of the SELECT statement and their processing order

A SELECT statement is written by listing clauses, as in SELECT columns FROM table WHERE condition GROUP BY columns HAVING condition ORDER BY columns. Unlike the written order, it is easier to understand the processing as FROM (choose and join tables), WHERE (select rows), GROUP BY (form groups), HAVING (select groups), SELECT (take columns and compute aggregate functions) and ORDER BY (sort). This is why aggregate functions cannot be written in WHERE and conditions on groups go in HAVING.

A declarative language, and handling NULL

SQL is a declarative language in which you write what result you want, and the DBMS optimizer decides the steps to obtain it. Care is needed with NULL, which represents an unknown value. A comparison with NULL is neither true nor false (unknown), so it is tested with IS NULL rather than = NULL. Aggregate functions such as SUM and AVG ignore NULL; COUNT(*) counts rows, while COUNT(column) counts rows where that column is not NULL.

Try it: questions about SQL

SQL statements are divided into the data definition language (DDL), data manipulation language (DML) and data control language (DCL). Which one is classified as DDL?

  1. AUPDATE employee SET dept = 'Dev' WHERE emp_no = 5
  2. BCREATE TABLE department (dept_id INTEGER PRIMARY KEY, dept_name CHAR(10))
  3. CGRANT SELECT ON employee TO tanaka
Show answer and explanation

Answer: B. CREATE TABLE department (dept_id INTEGER PRIMARY KEY, dept_name CHAR(10))

DDL (Data Definition Language) statements define the structure of a database, such as tables, views and indexes, and include CREATE, ALTER and DROP. DML (Data Manipulation Language) statements handle the data inside tables and include SELECT, INSERT, UPDATE and DELETE. DCL (Data Control Language) statements control things such as privileges and include GRANT, which gives a user privileges, and REVOKE, which takes them away. COMMIT, which makes a transaction permanent, and ROLLBACK, which cancels it, are sometimes counted as DCL and sometimes treated separately as transaction control statements. Among the choices, CREATE TABLE, which creates a table, is DDL.

What is the result of executing DELETE FROM employee in SQL without a WHERE clause?

  1. AAll rows of the employee table are deleted, and an empty table remains
  2. BThe table itself is deleted, and its definition is gone too
  3. CBecause there is no WHERE clause, an error occurs and nothing is deleted
Show answer and explanation

Answer: A. All rows of the employee table are deleted, and an empty table remains

DELETE is a DML statement that removes rows from a table. With a condition in a WHERE clause, only the rows that meet it are deleted; if the WHERE clause is omitted, every row is deleted. Even after DELETE removes all rows, the table definition (column names, types and constraints) remains, so rows can be added again with INSERT. Deleting the table itself, definition included, is done with the DDL statement DROP TABLE employee. The same applies to UPDATE: forgetting the WHERE clause rewrites every row, so it is important to check the condition before running it. Inside a transaction, the change can be cancelled with ROLLBACK before COMMIT.

FAQ

What is the difference between the WHERE and HAVING clauses in SQL?

The WHERE clause is a condition on individual rows before grouping, and aggregate functions cannot be written in it. The HAVING clause is a condition on the groups formed by GROUP BY, and it can use aggregate functions, as in HAVING SUM(amount) >= 1000.

How do you find rows containing NULL in SQL?

Write WHERE column IS NULL. WHERE column = NULL selects no rows, because a comparison with NULL is never true. Rows that are not NULL are selected with IS NOT NULL.

SQL in the explanations

Questions that mention SQL (15)

  1. SQL statements are divided into the data definition language (DDL), data manipulation language (DML) and data control language (DCL). Which one is classified as DDL?
  2. What is the result of executing DELETE FROM employee in SQL without a WHERE clause?
  3. From an employee table (emp_no, dept, salary), you want the departments whose average salary is 300 or more, together with that average. Which SQL statement is correct?
  4. From a customer table (cust_id, name) and an orders table (order_id, cust_id, amount), you want the names of customers who have never placed an order. Which SQL statement is correct?
  5. Among the isolation levels defined by the SQL standard, which correctly lists the phenomena that can occur under READ COMMITTED?
  6. Which hardware description languages are mainly used in RTL design?
  7. In a web application, entering a string such as ' OR '1'='1 in the password field of the login screen lets someone log in without knowing the password. What is this attack called?
  8. As a countermeasure against SQL injection, why are prepared statements with placeholders (parameterized queries) considered more fundamental than removing dangerous characters from the input?
  9. In relational database terminology, which term corresponds to one row of a table (relation)?
  10. In a relational database table, which condition must the column (or set of columns) designated as the primary key satisfy?
  11. An enrollment table (student_no, course_no, grade) records which student took which course and with what grade. One student takes several courses, and one course is taken by several students. If the same student never registers for the same course twice, which is an appropriate primary key for this table?
  12. The score column of an exam table contains four values: 80, NULL, 70, 90 (NULL means the person did not take the exam). What is the result of SELECT AVG(score) FROM exam?
  13. The stock count of a product in a stock table is 10. Transaction T1 reads the stock count and writes back the value minus 3, and T2 reads the stock count and writes back the value minus 5. If they run without locks in the order T1 reads, T2 reads, T1 writes, T2 writes, what is the final stock count?
  14. Among the problems that occur when transactions run concurrently, which correctly describes a dirty read?
  15. Transaction T1 ran SELECT COUNT(*) FROM orders WHERE amount >= 1000 and got 8. Immediately afterwards, T2 inserted an order row with an amount of 2000 and committed. When T1 ran the same SELECT statement again, it got 9. What is this phenomenon?

Books on Database Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Semiconductor, Electronics and Computing Calculators

Related terms

Transaction Isolation level Primary key SQL injection Relational database RTL (register-transfer level) Verilog Hardware description language (HDL) SystemVerilog ACID properties

Back to the glossary