# 20 SQL Interview Questions – Intern Level Part 1 (Questions 1 through 5)
Table of Contents
1. What is SQL and why is it important?
What is SQL?
SQL is a database query language used to interact with database management systems.
Why does SQL matter?
SQL is essential because it lets you query, update, and manage data in relational databases.
What is SQL used for?
SQL is used to query data, update data, manage databases, and perform other data-related operations.
Why is SQL important in programming?
SQL provides a standardized, powerful way to manipulate data in relational database systems, helping keep data consistent and secure.
2. Differentiate SQL and MySQL
SQL (Structured Query Language) is a standard query language used to interact with database management systems (DBMS). MySQL, meanwhile, is an open-source relational DBMS that relies on SQL as its query language.
Key differences between SQL and MySQL:
SQL:
- A standardized language for database queries.
- Not a specific database engine; it can be used with many DBMS implementations.
MySQL:
- An open-source relational DBMS.
- Uses SQL as its query language for working with data.
- Developed and maintained by Oracle Corporation.
In short, SQL is the general-purpose language for interacting with databases, while MySQL is a specific database system that uses SQL.
3. Explain the basic structure of an SQL query
A basic SQL query usually follows these clauses:
SELECT:
Choose the columns you want to retrieve. Use * to select every column.
SELECT column1, column2 FROM table_name;FROM:
Specify the table that holds the data.
SELECT column1, column2 FROM employees;WHERE:
Filter the data; only rows matching the condition are returned.
SELECT column1, column2 FROM employees WHERE department = 'IT';GROUP BY:
Group rows based on one or more columns.
SELECT department, COUNT(*) FROM employees GROUP BY department;HAVING:
Filter groups after GROUP BY; only groups that satisfy the condition remain.
SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 5;ORDER BY:
Sort the result set by one or more columns.
SELECT column1, column2 FROM employees ORDER BY column1 ASC;This is just a basic structure; you can extend or adjust the query depending on your needs.
4. List the types of SQL commands
DQL (Data Query Language):
SELECT: Query data from the database.
DML (Data Manipulation Language):
INSERT: Add new data into a table.UPDATE: Modify existing data in a table.DELETE: Remove data from a table.
DDL (Data Definition Language):
CREATE: Create databases, tables, indexes, or other objects.ALTER: Modify database, table, or other object structures.DROP: Delete databases, tables, indexes, or other objects.
DCL (Data Control Language):
GRANT: Give permissions to access database objects.REVOKE: Remove previously granted permissions.
TCL (Transaction Control Language):
COMMIT: Persist changes made in a transaction.ROLLBACK: Undo changes made in a transaction.SAVEPOINT: Mark a transaction point you can roll back to later.
Each command category serves a specific purpose: DQL for querying, DML for modifying data, DDL for managing structure, DCL for access control, and TCL for transaction control.
5. Define the SELECT statement and how to use it
Definition
The SELECT statement retrieves data from one or more tables in a database. It is a DQL (Data Query Language) command that lets you specify which columns to fetch and the conditions for returning data.
Usage
Basic syntax:
SELECT column1, column2, ...FROM table_nameWHERE condition;
SELECT: lists the columns to retrieve.
FROM: specifies the table containing the data.
WHERE(optional): filters the rows returned.
Examples:
-- Retrieve every column from the "employees" tableSELECT * FROM employees;
-- Retrieve specific columns from the "products" tableSELECT product_name, price FROM products WHERE category = 'Electronics';The
SELECTstatement is fundamental for exploring and analyzing SQL data, and you can customize it for a wide range of query scenarios.