sql
sql
in relational databases. Below is a list of basic and useful SQL commands that will help you
prepare for your Genpact Business Analyst interview:
1. SELECT
sql
CopyEdit
SELECT column1, column2
FROM table_name;
Example:
sql
CopyEdit
SELECT name, salary
FROM employees;
2. WHERE
sql
CopyEdit
SELECT column1, column2
FROM table_name
WHERE condition;
Example:
sql
CopyEdit
SELECT *
FROM employees
WHERE department = 'Finance';
sql
CopyEdit
SELECT *
FROM employees
WHERE salary > 50000 AND department = 'Finance';
4. ORDER BY
sql
CopyEdit
SELECT column1, column2
FROM table_name
ORDER BY column1 ASC; -- Or DESC
Example:
sql
CopyEdit
SELECT name, salary
FROM employees
ORDER BY salary DESC;
5. LIMIT / TOP
sql
CopyEdit
SELECT *
FROM employees
LIMIT 5; -- For MySQL
sql
CopyEdit
SELECT TOP 5 *
FROM employees;
Groups rows with the same values and allows filtering of aggregated data.
sql
CopyEdit
SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
7. JOINs
Inner Join:
sql
CopyEdit
SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.department_id;
Left Join:
sql
CopyEdit
SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
8. INSERT INTO
sql
CopyEdit
INSERT INTO table_name (column1, column2)
VALUES (value1, value2);
Example:
sql
CopyEdit
INSERT INTO employees (name, department, salary)
VALUES ('John', 'Finance', 60000);
9. UPDATE
sql
CopyEdit
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;
Example:
sql
CopyEdit
UPDATE employees
SET salary = 70000
WHERE name = 'John';
10. DELETE
sql
CopyEdit
DELETE FROM table_name
WHERE condition;
Example:
sql
CopyEdit
DELETE FROM employees
WHERE salary < 40000;
sql
CopyEdit
SELECT COUNT(*)
FROM employees;
sql
CopyEdit
SELECT SUM(salary)
FROM employees;
sql
CopyEdit
SELECT AVG(salary)
FROM employees;
4. MAX/MIN: Finds the highest or lowest value.
sql
CopyEdit
SELECT MAX(salary)
FROM employees;
12. ALIAS
sql
CopyEdit
SELECT name AS 'Employee Name', salary AS 'Income'
FROM employees;
13. SUBQUERY
sql
CopyEdit
SELECT name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
14. CASE
sql
CopyEdit
SELECT name,
CASE
WHEN salary > 50000 THEN 'High'
ELSE 'Low'
END AS Salary_Status
FROM employees;
sql
CopyEdit
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
Deletes a table.
sql
CopyEdit
DROP TABLE employees;
1. Focus on Data Analysis: Expect questions about retrieving and aggregating data (e.g.,
using GROUP BY, HAVING, and JOINs).
2. Real-World Scenarios: Be prepared to write SQL queries for business cases like sales
trends, employee performance, or financial summaries.
3. Optimization: Understand indexing basics and query optimization to handle large
datasets efficiently.
4. Practice Common Use Cases: Practice tasks like filtering, sorting, joining, and
analyzing data to answer behavioral questions during the interview.
Commands for Creating Tables and Using GROUP BY, HAVING, and JOINs
1. Create Table
The CREATE TABLE command is used to define a new table in a database. Here's an example:
sql
CopyEdit
CREATE TABLE employees (
employee_id INT PRIMARY KEY, -- Unique identifier for each employee
name VARCHAR(100), -- Employee name
department_id INT, -- Department ID (foreign key)
salary DECIMAL(10, 2), -- Employee salary
hire_date DATE -- Date of hiring
);
2. GROUP BY Command
The GROUP BY command groups rows that have the same values in specified columns and is
often used with aggregate functions.
sql
CopyEdit
SELECT department_id, SUM(salary) AS total_salary
FROM employees
GROUP BY department_id;
Explanation:
3. HAVING Command
The HAVING clause filters groups based on conditions, often used after GROUP BY.
sql
CopyEdit
SELECT department_id, SUM(salary) AS total_salary
FROM employees
GROUP BY department_id
HAVING SUM(salary) > 100000;
Explanation:
4. JOINs
Joins combine rows from two or more tables based on a related column.
sql
CopyEdit
SELECT e.name AS employee_name, d.department_name
FROM employees e
INNER JOIN departments d
ON e.department_id = d.department_id;
Explanation:
Left Join: Returns all rows from the left table and matched rows from the right table.
sql
CopyEdit
SELECT e.name AS employee_name, d.department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id;
Explanation:
Right Join: Returns all rows from the right table and matched rows from the left
table.
sql
CopyEdit
SELECT e.name AS employee_name, d.department_name
FROM employees e
RIGHT JOIN departments d
ON e.department_id = d.department_id;
Full Outer Join: Returns all rows when there’s a match in either table.
Example: Employees and their departments (include all employees and departments even if
there’s no match)
sql
CopyEdit
SELECT e.name AS employee_name, d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id;
Practice Scenario
Find the total salary by department, only showing departments with more than 5
employees, including department names.
sql
CopyEdit
SELECT d.department_name, COUNT(e.employee_id) AS employee_count,
SUM(e.salary) AS total_salary
FROM employees e
INNER JOIN departments d
ON e.department_id = d.department_id
GROUP BY d.department_name
HAVING COUNT(e.employee_id) > 5;
Let me know if you'd like me to explain any of these in more detail or create additional examples
for your preparation!
4o
You said:
how to view table
ChatGPT said:
ChatGPT
To view the structure or data of a table in SQL, use the following commands:
sql
CopyEdit
SELECT * FROM table_name;
Example:
sql
CopyEdit
SELECT * FROM employees;
This retrieves all rows and columns from the employees table.
2. View Table Structure (DESC or SHOW COLUMNS)
sql
CopyEdit
DESC table_name;
OR
sql
CopyEdit
SHOW COLUMNS FROM table_name;
Example:
sql
CopyEdit
DESC employees;
SQL Server
sql
CopyEdit
SP_COLUMNS table_name;
OR
sql
CopyEdit
EXEC sp_help 'table_name';
PostgreSQL
sql
CopyEdit
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'table_name';
sql
CopyEdit
SHOW CREATE TABLE table_name;
Example:
sql
CopyEdit
SHOW CREATE TABLE employees;