0% found this document useful (0 votes)
1 views

sql

Uploaded by

naityam.kalam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

sql

Uploaded by

naityam.kalam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

As a Business Analyst, understanding SQL is essential for retrieving and analyzing data stored

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

 Used to retrieve data from a table.

sql
CopyEdit
SELECT column1, column2
FROM table_name;

Example:

sql
CopyEdit
SELECT name, salary
FROM employees;

2. WHERE

 Filters records based on conditions.

sql
CopyEdit
SELECT column1, column2
FROM table_name
WHERE condition;

Example:

sql
CopyEdit
SELECT *
FROM employees
WHERE department = 'Finance';

3. AND, OR, NOT

 Combines multiple conditions in a query.

sql
CopyEdit
SELECT *
FROM employees
WHERE salary > 50000 AND department = 'Finance';

4. ORDER BY

 Sorts the result set in ascending or descending order.

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

 Limits the number of rows returned.

sql
CopyEdit
SELECT *
FROM employees
LIMIT 5; -- For MySQL

Example for SQL Server:

sql
CopyEdit
SELECT TOP 5 *
FROM employees;

6. GROUP BY and HAVING

 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

 Combines rows from two or more tables.

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

 Adds new rows to a table.

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

 Updates existing records in a table.

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

 Deletes records from a table.

sql
CopyEdit
DELETE FROM table_name
WHERE condition;

Example:

sql
CopyEdit
DELETE FROM employees
WHERE salary < 40000;

11. AGGREGATE FUNCTIONS

 Perform calculations on multiple rows.

1. COUNT: Counts rows.

sql
CopyEdit
SELECT COUNT(*)
FROM employees;

2. SUM: Adds values.

sql
CopyEdit
SELECT SUM(salary)
FROM employees;

3. AVG: Calculates average.

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

 Renames columns or tables for better readability.

sql
CopyEdit
SELECT name AS 'Employee Name', salary AS 'Income'
FROM employees;

13. SUBQUERY

 A query inside another query.

sql
CopyEdit
SELECT name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

14. CASE

 Adds conditional logic to queries.

sql
CopyEdit
SELECT name,
CASE
WHEN salary > 50000 THEN 'High'
ELSE 'Low'
END AS Salary_Status
FROM employees;

15. CREATE TABLE

 Creates a new table.

sql
CopyEdit
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(50),
salary DECIMAL(10, 2)
);

16. DROP TABLE

 Deletes a table.

sql
CopyEdit
DROP TABLE employees;

Key Tips for Genpact Interview

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
);

CREATE TABLE departments (


department_id INT PRIMARY KEY, -- Unique identifier for each department
department_name VARCHAR(50) -- Name of the department
);

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.

Example: Total salary paid per department

sql
CopyEdit
SELECT department_id, SUM(salary) AS total_salary
FROM employees
GROUP BY department_id;

Explanation:

 SUM(salary) calculates the total salary for each department_id.


 GROUP BY department_id ensures the results are grouped by departments.

3. HAVING Command

The HAVING clause filters groups based on conditions, often used after GROUP BY.

Example: Departments with total salary greater than 100,000

sql
CopyEdit
SELECT department_id, SUM(salary) AS total_salary
FROM employees
GROUP BY department_id
HAVING SUM(salary) > 100000;

Explanation:

 HAVING applies conditions to the aggregated data after grouping.


 This query only returns departments where the total salary exceeds 100,000.

4. JOINs

Joins combine rows from two or more tables based on a related column.

Inner Join: Returns rows where there is a match in both tables.

Example: List all employees with their department names

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:

 INNER JOIN matches rows in employees and departments where department_id


matches.

Left Join: Returns all rows from the left table and matched rows from the right table.

Example: Employees and their departments (include employees with no department)

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:

 If an employee doesn’t belong to a department, department_name will return NULL.

Right Join: Returns all rows from the right table and matched rows from the left
table.

Example: All departments and their employees

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:

1. View Table Data (SELECT *)

To see all records in a table, use:

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)

To check the column names, data types, and constraints:

MySQL / MariaDB / Oracle

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';

3. View Table Creation Query (SHOW CREATE TABLE)

For MySQL/MariaDB, use:

sql
CopyEdit
SHOW CREATE TABLE table_name;

Example:

sql
CopyEdit
SHOW CREATE TABLE employees;

This displays the SQL query used to create the table.

You might also like