Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views6 pages

SQL Basics and Examples With 15 Interview Quest With Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

SQL Basics and Examples With 15 Interview Quest With Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SQL Basics and Examples

1. SQL Data Types:


SQL supports various data types to store different kinds of data. Common data
types include:

INT : Integer values.

VARCHAR(n) : Variable-length character strings with a maximum length of n.

DECIMAL(p, s): Fixed-point decimal numbers with p total digits and s digits after the
decimal point.

Example:

CREATE TABLE Employees (


id INT,
name VARCHAR(50),
age INT,
salary DECIMAL(10, 2)
);

2. Primary Key:
A primary key uniquely identifies each record in a table. It ensures data integrity and
facilitates efficient data retrieval.

Example:

CREATE TABLE Employees (


id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
salary DECIMAL(10, 2)
);

3. Foreign Key:
A foreign key establishes a relationship between two tables, ensuring referential
integrity. It references the primary key of another table.

SQL Basics and Examples 1


Example:

CREATE TABLE Departments (


id INT PRIMARY KEY,
name VARCHAR(50)
);

CREATE TABLE Employees (


id INT PRIMARY KEY,
name VARCHAR(50),
department_id INT,
FOREIGN KEY (department_id) REFERENCES Departments(id)
);

4. Indexes:
Indexes improve the performance of database queries by allowing faster data
retrieval. They can be created on one or multiple columns.

Example:

CREATE INDEX idx_employees_name ON Employees (name);

5. Aggregation Functions:
SQL provides functions to perform calculations on data. Common aggregate
functions include SUM , COUNT , AVG , MIN , and MAX .

Example:

SELECT COUNT(*) AS total_employees


FROM Employees;

6. Subqueries:
Subqueries are queries nested within other queries. They allow you to retrieve data
based on the results of another query.

Example:

SELECT name
FROM Employees
WHERE department_id IN (SELECT id FROM Departments WHERE name = 'Sales');

SQL Basics and Examples 2


7. Transactions:
Transactions ensure that a group of database operations are executed atomically,
either all succeeding or all failing.

Example:

BEGIN TRANSACTION;
-- SQL statements --
COMMIT;

8. Views:
Views are virtual tables that are derived from the result of a query. They can simplify
complex queries and provide data security.

Example:

CREATE VIEW SalesEmployees AS


SELECT name, salary
FROM Employees
WHERE department_id IN (SELECT id FROM Departments WHERE name = 'Sales');

These are just a few SQL concepts and examples. SQL is a powerful language with
many features and functionalities. Exploring these concepts further and practicing with
real-world scenarios will enhance your SQL skills.

Here are 15 common SQL interview questions along with example code:
Q1 : What is the difference between HAVING and WHERE clauses in SQL?

A: The WHERE clause filters rows before grouping and aggregation, while the HAVING

clause filters the results after grouping and aggregation.


Q2 : Write a query to fetch all employees from the "Employees" table.

A :

SELECT * FROM Employees;

SQL Basics and Examples 3


Q3 : What is a self-join? Provide an example.
A : A self-join is when a table is joined with itself. It is useful when you want to combine
rows with related information.

SELECT e1.name, e2.name


FROM Employees e1
INNER JOIN Employees e2 ON e1.manager_id = e2.id;

Q4 : What are aggregate functions in SQL? Provide examples.


A : Aggregate functions perform calculations on a set of values and return a single
value. Examples include COUNT , SUM , AVG , MIN , and MAX .

SELECT COUNT(*) FROM Employees;


SELECT SUM(salary) FROM Employees;

Q5 : Explain the difference between INNER JOIN , LEFT JOIN , and RIGHT JOIN .
A : INNER JOIN returns only matching rows from both tables, LEFT JOIN returns all rows
from the left table and matching rows from the right table, and RIGHT JOIN returns all
rows from the right table and matching rows from the left table.
Q6 : How can you prevent duplicate rows from appearing in a result set?

A : Use the DISTINCT keyword to eliminate duplicate rows from the result set.

SELECT DISTINCT department FROM Employees;

Q7 : What is a subquery? Provide an example.


A : A subquery is a query nested within another query. It is used to retrieve data based
on the results of another query.

SELECT name, salary


FROM Employees
WHERE department_id IN (SELECT id FROM Departments WHERE name = 'Sales');

Q8 : What is a primary key?


A : A primary key is a unique identifier for each row in a table. It ensures data integrity

SQL Basics and Examples 4


and allows for efficient data retrieval.

CREATE TABLE Employees (


id INT PRIMARY KEY,
name VARCHAR(50)
);

Q9 : Explain the purpose of the GROUP BY clause.

A : The GROUP BY clause is used to group rows based on one or more columns. It is
typically used with aggregate functions to perform calculations on each group.

SELECT department_id, AVG(salary)


FROM Employees
GROUP BY department_id;

Q10 : What is a view in SQL? Provide an example.


A : A view is a virtual table derived from the result of a query. It allows for simplifying
complex queries and provides an extra layer of security.

CREATE VIEW SalesEmployees AS


SELECT name, salary
FROM Employees
WHERE department_id = 1;

Q11 : How can you sort the result set in descending order?

A : : Use the ORDER BY clause with the DESC keyword.

SELECT name, salary


FROM Employees
ORDER BY salary DESC;

Q12 : What is a foreign key?


A : A foreign key establishes a relationship between two tables. It references the
primary key of another table and ensures referential integrity

SQL Basics and Examples 5


CREATE TABLE Orders (
id INT PRIMARY KEY,
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES Customers(id)
);

Q13 : What is the purpose of the LIKE operator?

A: The LIKE operator is used for pattern matching in SQL. It allows you to search for a
specified pattern in a column.

SELECT name
FROM Employees
WHERE name LIKE 'J%';

Q14 : How do you update records in a table?

A: Use the UPDATE statement to modify existing records in a table.

UPDATE Employees
SET salary = 6000
WHERE id = 1;

Q15: How do you delete records from a table?

A: Use the DELETE FROM statement to remove records from a table.

DELETE FROM Employees


WHERE age > 65;

These are just a few examples of SQL interview questions. It's important to practice and
review a variety of SQL concepts and query scenarios to prepare thoroughly for an
interview.

SQL Basics and Examples 6

You might also like