Basic-level SQL interview questions for Freshers / Entry Level positions.
1. What is SQL and what are its main components?
Answer: SQL (Structured Query Language) is used to communicate with databases. Main components: DDL (CREATE, ALTER, DROP - structure), DML (SELECT, INSERT, UPDATE, DELETE - data), DCL (GRANT, REVOKE - permissions), TCL (COMMIT, ROLLBACK - transactions).
-- DDL: Create structure CREATE TABLE students (id INT, name VARCHAR(100)); -- DML: Manipulate data INSERT INTO students VALUES (1, 'John'); SELECT * FROM students; -- DCL: Control access GRANT SELECT ON students TO app_user; -- TCL: Transaction control BEGIN; UPDATE students SET name = 'Jane' WHERE id = 1; COMMIT;
2. What is the difference between CHAR and VARCHAR?
Answer: CHAR is fixed-length (pads with spaces), VARCHAR is variable-length (stores actual length). CHAR(10) always uses 10 bytes, VARCHAR(10) uses actual string length + overhead. Use CHAR for fixed-length data (country codes), VARCHAR for variable (names).
CREATE TABLE example ( country_code CHAR(2), -- Always 2 chars: 'US', 'UK' name VARCHAR(100) -- Variable: 'John' uses 4 chars ); -- CHAR pads with spaces SELECT LENGTH(country_code) FROM example; -- Returns 2 SELECT country_code = 'US' FROM example; -- Works (space-padded comparison)
3. What is NULL in SQL and how do you check for it?
Answer: NULL represents unknown or missing value. It's not equal to zero or empty string. Use IS NULL or IS NOT NULL to check (= NULL doesn't work). NULL in any calculation returns NULL. Use COALESCE() or IFNULL() to provide defaults.
-- Checking for NULL SELECT * FROM users WHERE phone IS NULL; SELECT * FROM users WHERE phone IS NOT NULL; -- WRONG: This never matches SELECT * FROM users WHERE phone = NULL; -- Handling NULL in calculations SELECT COALESCE(phone, 'N/A') FROM users; SELECT salary + COALESCE(bonus, 0) AS total FROM employees;