Data Definition Language Ddl Interview Questions
Comprehensive data definition language ddl interview questions and answers for SQL. Prepare for your next job interview with expert guidance.
Questions Overview
1. What is DDL in SQL and what are its main commands?
Basic2. Explain the CREATE TABLE statement in SQL.
Basic3. What are table constraints in SQL?
Moderate4. How do you use the ALTER TABLE statement?
Moderate5. Explain the DROP TABLE statement.
Basic6. What is the difference between DROP and TRUNCATE?
Moderate7. How do you create an index in SQL?
Moderate8. Explain the concept of a schema in SQL.
Advanced9. What is a view in SQL and how do you create one?
Moderate10. How do you define a PRIMARY KEY constraint?
Basic11. Explain the FOREIGN KEY constraint.
Moderate12. What is the UNIQUE constraint in SQL?
Basic13. How do you create a temporary table?
Moderate14. Explain the CHECK constraint.
Moderate15. What is a DEFAULT constraint?
Basic16. How do you rename a table in SQL?
Moderate17. Explain composite keys in SQL.
Advanced18. What are auto-increment columns?
Moderate19. Explain the concept of table inheritance in SQL.
Advanced20. What is a clustered index?
Advanced21. How do you create a sequence in SQL?
Moderate22. Explain database normalization in DDL context.
Advanced23. What are computed or generated columns?
Advanced24. How do you create a partitioned table?
Advanced25. Explain the cascading actions in foreign key constraints.
Advanced26. What is the purpose of data types in table creation?
Basic27. How do you create a materialized view?
Advanced1. What is DDL in SQL and what are its main commands?
BasicDDL (Data Definition Language) is a subset of SQL used to define and manage database structures. The main DDL commands are CREATE, ALTER, DROP, and TRUNCATE, which are used to create, modify, delete, and remove database objects like tables, indexes, and schemas.
2. Explain the CREATE TABLE statement in SQL.
BasicThe CREATE TABLE statement is used to create a new table in a database. It specifies the table name, column names, data types, and optional constraints. For example: CREATE TABLE employees (id INT PRIMARY KEY, name VARCHAR(100), salary DECIMAL(10,2)).
3. What are table constraints in SQL?
ModerateTable constraints are rules enforced on data columns to maintain data integrity. Common constraints include PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, CHECK, and DEFAULT. They define rules for data that can be inserted into a table.
4. How do you use the ALTER TABLE statement?
ModerateALTER TABLE is used to modify an existing table structure. Common operations include adding, modifying, or dropping columns, adding or removing constraints. For example: ALTER TABLE employees ADD COLUMN email VARCHAR(100), or ALTER TABLE employees DROP COLUMN phone_number.
5. Explain the DROP TABLE statement.
BasicDROP TABLE completely removes a table and all its data from the database. For example: DROP TABLE employees. This command permanently deletes the table structure and all associated data, and cannot be undone unless you have a backup.
6. What is the difference between DROP and TRUNCATE?
ModerateDROP removes the entire table structure and data, while TRUNCATE removes all rows from a table but keeps the table structure intact. DROP is a DDL command that deletes the table, TRUNCATE quickly removes all data without logging individual row deletions.
7. How do you create an index in SQL?
ModerateIndexes are created using the CREATE INDEX statement. For example: CREATE INDEX idx_last_name ON employees(last_name). Indexes improve query performance by allowing faster data retrieval. They can be unique or non-unique and can be created on one or multiple columns.
8. Explain the concept of a schema in SQL.
AdvancedA schema is a named collection of database objects like tables, views, indexes, and stored procedures. It provides a way to logically group and organize database objects. You can create a schema using CREATE SCHEMA statement and manage object ownership and permissions.
9. What is a view in SQL and how do you create one?
ModerateA view is a virtual table based on the result of a SELECT statement. It doesn't store data physically but provides a way to simplify complex queries. Created using CREATE VIEW: CREATE VIEW high_salary_employees AS SELECT * FROM employees WHERE salary > 50000.
10. How do you define a PRIMARY KEY constraint?
BasicA PRIMARY KEY constraint uniquely identifies each record in a table. It can be defined during table creation: CREATE TABLE employees (id INT PRIMARY KEY, name VARCHAR(100)), or added later: ALTER TABLE employees ADD PRIMARY KEY (id).
11. Explain the FOREIGN KEY constraint.
ModerateA FOREIGN KEY creates a relationship between two tables by referencing the primary key of another table. It ensures referential integrity. Example: CREATE TABLE orders (id INT, customer_id INT, FOREIGN KEY (customer_id) REFERENCES customers(id)).
12. What is the UNIQUE constraint in SQL?
BasicThe UNIQUE constraint ensures that all values in a column are different. Unlike PRIMARY KEY, a table can have multiple UNIQUE constraints. Example: CREATE TABLE users (id INT, email VARCHAR(100) UNIQUE).
13. How do you create a temporary table?
ModerateTemporary tables are created using CREATE TEMPORARY TABLE or CREATE TEMP TABLE. They exist only for the duration of a session. Example: CREATE TEMPORARY TABLE temp_sales (product_id INT, total_sales DECIMAL).
14. Explain the CHECK constraint.
ModerateThe CHECK constraint is used to limit the value range that can be placed in a column. Example: CREATE TABLE employees (age INT CHECK (age >= 18 AND age <= 65)), which ensures the age is between 18 and 65.
15. What is a DEFAULT constraint?
BasicThe DEFAULT constraint provides a default value for a column when no value is specified. Example: CREATE TABLE products (id INT, price DECIMAL DEFAULT 0.00), which sets the default price to 0 if not explicitly provided.
16. How do you rename a table in SQL?
ModerateTable renaming can be done using ALTER TABLE. The exact syntax varies between database systems. For example, in MySQL: RENAME TABLE old_table TO new_table; in SQL Server: SP_RENAME 'old_table', 'new_table'.
17. Explain composite keys in SQL.
AdvancedA composite key is a primary key composed of multiple columns. It's used when a single column cannot uniquely identify a record. Example: CREATE TABLE order_items (order_id INT, product_id INT, PRIMARY KEY (order_id, product_id)).
18. What are auto-increment columns?
ModerateAuto-increment columns automatically generate unique numeric values when a new record is inserted. Syntax varies by database: MySQL uses AUTO_INCREMENT, SQL Server uses IDENTITY, PostgreSQL uses SERIAL.
19. Explain the concept of table inheritance in SQL.
AdvancedTable inheritance allows creating a new table based on an existing table, inheriting its columns and characteristics. This is supported differently across database systems, with PostgreSQL providing direct support for table inheritance.
20. What is a clustered index?
AdvancedA clustered index determines the physical order of data in a table. Each table can have only one clustered index. It sorts and stores the data rows in the table based on their key values, which affects the way data is physically stored.
21. How do you create a sequence in SQL?
ModerateA sequence is a database object that generates a series of numeric values. Example: CREATE SEQUENCE emp_seq START WITH 1 INCREMENT BY 1. It can be used to generate unique identifier values for tables.
22. Explain database normalization in DDL context.
AdvancedDatabase normalization is the process of organizing data to reduce redundancy and improve data integrity. It involves creating tables, defining relationships, and applying constraints to minimize data duplication and potential anomalies.
23. What are computed or generated columns?
AdvancedComputed columns are columns whose values are calculated dynamically from other columns in the table. Example: total_price DECIMAL GENERATED ALWAYS AS (quantity * unit_price) STORED.
24. How do you create a partitioned table?
AdvancedTable partitioning divides large tables into smaller, more manageable pieces. The syntax varies by database system. It allows improving query performance and management of large datasets by splitting them into logical segments.
25. Explain the cascading actions in foreign key constraints.
AdvancedCascading actions define what happens to dependent records when a referenced record is updated or deleted. Options include CASCADE (propagate changes), SET NULL, SET DEFAULT, and NO ACTION.
26. What is the purpose of data types in table creation?
BasicData types define the type of data a column can store, its size, and potential constraints. They ensure data integrity, optimize storage, and define how data can be processed and manipulated in the database.
27. How do you create a materialized view?
AdvancedA materialized view is a database object that contains the results of a query. Unlike regular views, it stores the query results physically. Syntax varies by database system, but generally involves CREATE MATERIALIZED VIEW with a SELECT statement.