๐ฅ๐These SQL interview questions typically asked in a Data Analyst interview?
1.What distinguishes a Primary key from a Unique key?
Primary key uniquely identifies each record in a table and cannot contain null values, whereas a Unique key also uniquely identifies records but can contain null values and multiple unique keys can exist in a table.
2. Define Candidate key.
Candidate key is a key or set of keys that uniquely identifies each record in a table. It can be a combination of Primary and Alternate keys.
3.Explain the concept of Constraint in SQL.
A Constraint is a specific rule or limit defined in a table to enforce data integrity. Examples include NOT NULL and AUTO INCREMENT.
4. Differentiate between TRUNCATE and DELETE commands.
TRUNCATE is a DDL command that removes all data from a table while preserving the table's structure, and it is faster than DELETE. DELETE is a DML command that removes specific rows based on conditions and operates slower than TRUNCATE as it deletes data row by row.
5.Compare and contrast a 'View' and a 'Stored Procedure'.
A View is a virtual table derived from one or more base tables, often used to simplify complex queries, while a Stored Procedure is a precompiled collection of SQL statements stored on the database server, used to perform specific tasks or operations.
6.What sets apart a Common Table Expression from a temporary table?
A Common Table Expression (CTE) is a temporary result set defined within the execution scope of a single SELECT, DELETE, or UPDATE statement, while a temporary table is stored in TempDB and persists until the session ends.
7.Contrast a clustered index with a non-clustered index.
A clustered index determines the physical ordering of data in a table and there can be only one clustered index per table. In contrast, a non-clustered index is similar to an index in a book where data is stored separately from the index, and multiple non-clustered indexes can exist for a table.
8.Define triggers in SQL and their purpose.
Triggers are SQL codes that automatically execute in response to certain events on a table, such as INSERT, UPDATE, or DELETE operations. They are used to maintain data integrity and perform actions based on specific conditions.
1.What distinguishes a Primary key from a Unique key?
Primary key uniquely identifies each record in a table and cannot contain null values, whereas a Unique key also uniquely identifies records but can contain null values and multiple unique keys can exist in a table.
2. Define Candidate key.
Candidate key is a key or set of keys that uniquely identifies each record in a table. It can be a combination of Primary and Alternate keys.
3.Explain the concept of Constraint in SQL.
A Constraint is a specific rule or limit defined in a table to enforce data integrity. Examples include NOT NULL and AUTO INCREMENT.
4. Differentiate between TRUNCATE and DELETE commands.
TRUNCATE is a DDL command that removes all data from a table while preserving the table's structure, and it is faster than DELETE. DELETE is a DML command that removes specific rows based on conditions and operates slower than TRUNCATE as it deletes data row by row.
5.Compare and contrast a 'View' and a 'Stored Procedure'.
A View is a virtual table derived from one or more base tables, often used to simplify complex queries, while a Stored Procedure is a precompiled collection of SQL statements stored on the database server, used to perform specific tasks or operations.
6.What sets apart a Common Table Expression from a temporary table?
A Common Table Expression (CTE) is a temporary result set defined within the execution scope of a single SELECT, DELETE, or UPDATE statement, while a temporary table is stored in TempDB and persists until the session ends.
7.Contrast a clustered index with a non-clustered index.
A clustered index determines the physical ordering of data in a table and there can be only one clustered index per table. In contrast, a non-clustered index is similar to an index in a book where data is stored separately from the index, and multiple non-clustered indexes can exist for a table.
8.Define triggers in SQL and their purpose.
Triggers are SQL codes that automatically execute in response to certain events on a table, such as INSERT, UPDATE, or DELETE operations. They are used to maintain data integrity and perform actions based on specific conditions.
๐16โค9
๐ ๐ผ๐๐ ๐๐๐ธ๐ฒ๐ฑ ๐ฆ๐ค๐ ๐๐ป๐๐ฒ๐ฟ๐๐ถ๐ฒ๐ ๐ค๐๐ฒ๐๐๐ถ๐ผ๐ป๐ ๐ฎ๐ ๐ ๐๐๐ก๐ ๐๐ผ๐บ๐ฝ๐ฎ๐ป๐ถ๐ฒ๐๐ฅ๐ฅ
1. How do you retrieve all columns from a table?
SELECT * FROM table_name;
2. What SQL statement is used to filter records?
SELECT * FROM table_name
WHERE condition;
The WHERE clause is used to filter records based on a specified condition.
3. How can you join multiple tables? Describe different types of JOINs.
SELECT columns
FROM table1
JOIN table2 ON table1.column = table2.column
JOIN table3 ON table2.column = table3.column;
Types of JOINs:
1. INNER JOIN: Returns records with matching values in both tables
SELECT * FROM table1
INNER JOIN table2 ON table1.column = table2.column;
2. LEFT JOIN (or LEFT OUTER JOIN): Returns all records from the left table and matched records from the right table. Unmatched records will have NULL values.
SELECT * FROM table1
LEFT JOIN table2 ON table1.column = table2.column;
3. RIGHT JOIN (or RIGHT OUTER JOIN): Returns all records from the right table and matched records from the left table. Unmatched records will have NULL values.
SELECT * FROM table1
RIGHT JOIN table2 ON table1.column = table2.column;
4. FULL JOIN (or FULL OUTER JOIN): Returns records when there is a match in either left or right table. Unmatched records will have NULL values.
SELECT * FROM table1
FULL JOIN table2 ON table1.column = table2.column;
4. What is the difference between WHERE and HAVING clauses?
WHERE: Filters records before any groupings are made.
SELECT * FROM table_name
WHERE condition;
HAVING: Filters records after groupings are made.
SELECT column, COUNT(*)
FROM table_name
GROUP BY column
HAVING COUNT(*) > value;
5. How do you count the number of records in a table?
SELECT COUNT(*) FROM table_name;
This query counts all the records in the specified table.
6. How do you calculate average, sum, minimum, and maximum values in a column?
Average: SELECT AVG(column_name) FROM table_name;
Sum: SELECT SUM(column_name) FROM table_name;
Minimum: SELECT MIN(column_name) FROM table_name;
Maximum: SELECT MAX(column_name) FROM table_name;
7. What is a subquery, and how do you use it?
Subquery: A query nested inside another query
SELECT * FROM table_name
WHERE column_name = (SELECT column_name FROM another_table WHERE condition);
Till then keep learning and keep exploring ๐
1. How do you retrieve all columns from a table?
SELECT * FROM table_name;
2. What SQL statement is used to filter records?
SELECT * FROM table_name
WHERE condition;
The WHERE clause is used to filter records based on a specified condition.
3. How can you join multiple tables? Describe different types of JOINs.
SELECT columns
FROM table1
JOIN table2 ON table1.column = table2.column
JOIN table3 ON table2.column = table3.column;
Types of JOINs:
1. INNER JOIN: Returns records with matching values in both tables
SELECT * FROM table1
INNER JOIN table2 ON table1.column = table2.column;
2. LEFT JOIN (or LEFT OUTER JOIN): Returns all records from the left table and matched records from the right table. Unmatched records will have NULL values.
SELECT * FROM table1
LEFT JOIN table2 ON table1.column = table2.column;
3. RIGHT JOIN (or RIGHT OUTER JOIN): Returns all records from the right table and matched records from the left table. Unmatched records will have NULL values.
SELECT * FROM table1
RIGHT JOIN table2 ON table1.column = table2.column;
4. FULL JOIN (or FULL OUTER JOIN): Returns records when there is a match in either left or right table. Unmatched records will have NULL values.
SELECT * FROM table1
FULL JOIN table2 ON table1.column = table2.column;
4. What is the difference between WHERE and HAVING clauses?
WHERE: Filters records before any groupings are made.
SELECT * FROM table_name
WHERE condition;
HAVING: Filters records after groupings are made.
SELECT column, COUNT(*)
FROM table_name
GROUP BY column
HAVING COUNT(*) > value;
5. How do you count the number of records in a table?
SELECT COUNT(*) FROM table_name;
This query counts all the records in the specified table.
6. How do you calculate average, sum, minimum, and maximum values in a column?
Average: SELECT AVG(column_name) FROM table_name;
Sum: SELECT SUM(column_name) FROM table_name;
Minimum: SELECT MIN(column_name) FROM table_name;
Maximum: SELECT MAX(column_name) FROM table_name;
7. What is a subquery, and how do you use it?
Subquery: A query nested inside another query
SELECT * FROM table_name
WHERE column_name = (SELECT column_name FROM another_table WHERE condition);
Till then keep learning and keep exploring ๐
๐52โค9๐คฃ1
After learning SQL ๐๐
https://www.instagram.com/reel/C-RnCrTSrqn/?igsh=MWk4cnVqa2VxZXZpaw==
https://www.instagram.com/reel/C-RnCrTSrqn/?igsh=MWk4cnVqa2VxZXZpaw==
๐คฃ10๐7
Complete 14-day roadmap to learn SQL learning:
Day 1: Introduction to Databases
- Understand the concept of databases and their importance.
- Learn about relational databases and SQL.
- Explore the basic structure of SQL queries.
Day 2: Basic SQL Syntax
- Learn SQL syntax: statements, clauses, and keywords.
- Understand the SELECT statement for retrieving data.
- Practice writing basic SELECT queries with conditions and filters.
Day 3: Retrieving Data from Multiple Tables
- Learn about joins: INNER JOIN, LEFT JOIN, RIGHT JOIN.
- Understand how to retrieve data from multiple tables using joins.
- Practice writing queries involving multiple tables.
Day 4: Aggregate Functions
- Learn about aggregate functions: COUNT, SUM, AVG, MIN, MAX.
- Understand how to use aggregate functions to perform calculations on data.
- Practice writing queries with aggregate functions.
Day 5: Subqueries
- Learn about subqueries and their role in SQL.
- Understand how to use subqueries in SELECT, WHERE, and FROM clauses.
- Practice writing queries with subqueries.
Day 6: Data Manipulation Language (DML)
- Learn about DML commands: INSERT, UPDATE, DELETE.
- Understand how to add, modify, and delete data in a database.
- Practice writing DML statements.
Day 7: Data Definition Language (DDL)
- Learn about DDL commands: CREATE TABLE, ALTER TABLE, DROP TABLE.
- Understand constraints: PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL.
- Practice designing database schemas and creating tables.
Day 8: Data Control Language (DCL)
- Learn about DCL commands: GRANT, REVOKE for managing user permissions.
- Understand how to control access to database objects.
- Practice granting and revoking permissions.
Day 9: Transactions
- Understand the concept of transactions in SQL.
- Learn about transaction control commands: COMMIT, ROLLBACK.
- Practice managing transactions.
Day 10: Views
- Learn about views and their benefits.
- Understand how to create, modify, and drop views.
- Practice creating views.
Day 11: Indexes
- Learn about indexes and their role in database optimization.
- Understand different types of indexes (e.g., B-tree, hash).
- Practice creating and managing indexes.
Day 12: Optimization Techniques
- Explore optimization techniques such as query tuning and normalization.
- Understand the importance of database design for optimization.
- Practice optimizing SQL queries.
Day 13: Review and Practice
- Review all concepts covered in the previous days.
- Work on sample projects or exercises to reinforce learning.
- Take practice quizzes or tests.
Day 14: Final Review and Projects
- Review all concepts learned throughout the 14 days.
- Work on a final project to apply SQL knowledge.
- Seek out additional resources or tutorials if needed.
Here are some practical SQL syntax examples for each day of your learning journey:
Day 1: Introduction to Databases
- Syntax to select all columns from a table:
Day 2: Basic SQL Syntax
- Syntax to select specific columns from a table:
Day 3: Retrieving Data from Multiple Tables
- Syntax for INNER JOIN to retrieve data from two tables:
Day 4: Aggregate Functions
- Syntax for COUNT to count the number of rows in a table:
Day 5: Subqueries
- Syntax for using a subquery in the WHERE clause:
Day 6: Data Manipulation Language (DML)
- Syntax for INSERT to add data into a table:
Day 1: Introduction to Databases
- Understand the concept of databases and their importance.
- Learn about relational databases and SQL.
- Explore the basic structure of SQL queries.
Day 2: Basic SQL Syntax
- Learn SQL syntax: statements, clauses, and keywords.
- Understand the SELECT statement for retrieving data.
- Practice writing basic SELECT queries with conditions and filters.
Day 3: Retrieving Data from Multiple Tables
- Learn about joins: INNER JOIN, LEFT JOIN, RIGHT JOIN.
- Understand how to retrieve data from multiple tables using joins.
- Practice writing queries involving multiple tables.
Day 4: Aggregate Functions
- Learn about aggregate functions: COUNT, SUM, AVG, MIN, MAX.
- Understand how to use aggregate functions to perform calculations on data.
- Practice writing queries with aggregate functions.
Day 5: Subqueries
- Learn about subqueries and their role in SQL.
- Understand how to use subqueries in SELECT, WHERE, and FROM clauses.
- Practice writing queries with subqueries.
Day 6: Data Manipulation Language (DML)
- Learn about DML commands: INSERT, UPDATE, DELETE.
- Understand how to add, modify, and delete data in a database.
- Practice writing DML statements.
Day 7: Data Definition Language (DDL)
- Learn about DDL commands: CREATE TABLE, ALTER TABLE, DROP TABLE.
- Understand constraints: PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL.
- Practice designing database schemas and creating tables.
Day 8: Data Control Language (DCL)
- Learn about DCL commands: GRANT, REVOKE for managing user permissions.
- Understand how to control access to database objects.
- Practice granting and revoking permissions.
Day 9: Transactions
- Understand the concept of transactions in SQL.
- Learn about transaction control commands: COMMIT, ROLLBACK.
- Practice managing transactions.
Day 10: Views
- Learn about views and their benefits.
- Understand how to create, modify, and drop views.
- Practice creating views.
Day 11: Indexes
- Learn about indexes and their role in database optimization.
- Understand different types of indexes (e.g., B-tree, hash).
- Practice creating and managing indexes.
Day 12: Optimization Techniques
- Explore optimization techniques such as query tuning and normalization.
- Understand the importance of database design for optimization.
- Practice optimizing SQL queries.
Day 13: Review and Practice
- Review all concepts covered in the previous days.
- Work on sample projects or exercises to reinforce learning.
- Take practice quizzes or tests.
Day 14: Final Review and Projects
- Review all concepts learned throughout the 14 days.
- Work on a final project to apply SQL knowledge.
- Seek out additional resources or tutorials if needed.
Here are some practical SQL syntax examples for each day of your learning journey:
Day 1: Introduction to Databases
- Syntax to select all columns from a table:
SELECT * FROM table_name;
Day 2: Basic SQL Syntax
- Syntax to select specific columns from a table:
SELECT column1, column2 FROM table_name;
Day 3: Retrieving Data from Multiple Tables
- Syntax for INNER JOIN to retrieve data from two tables:
SELECT orders.order_id, customers.customer_name
FROM orders
INNER JOIN customers ON orders.customer_id = customers.customer_id;
Day 4: Aggregate Functions
- Syntax for COUNT to count the number of rows in a table:
SELECT COUNT(*) FROM table_name;
Day 5: Subqueries
- Syntax for using a subquery in the WHERE clause:
SELECT column1, column2
FROM table_name
WHERE column1 IN (SELECT column1 FROM another_table WHERE condition);
Day 6: Data Manipulation Language (DML)
- Syntax for INSERT to add data into a table:
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
๐43โค16
Programming languages in data science ๐๐
https://www.instagram.com/reel/C-YLuq6yI_6/?igsh=Ynd3aHY4bWlsOW00
https://www.instagram.com/reel/C-YLuq6yI_6/?igsh=Ynd3aHY4bWlsOW00
๐8
Majority of top companies hiring for analytic roles (Data Analyst/Business Analyst) focus heavily on SQL understanding as a selection criteria, which according to me, should be the first thing you start your preparation with.
I have divided this SQL roadmap into 3 steps (Basics, Level Up & Practice), and it should take around 1 month to complete.
Step 1 - Basics ๐ข :
โกWhat is a Relational Database / RDBMS?
โกSQL Data Types - Varchar, text, int, number, date, float, boolean.
โกSQL commands - select, where, like, distinct, between, group by, having, order by, insert into, case when, update, truncate, delete, commit, rollback (basically all the DDL, DML, DCL, TCL commands in SQL).
โกIntegrity Constraints - Primary key, foreign key, not null, unique.
โกOperators arithmetic, logical, and comparison operations.
โกUse of distinct, order by, limit, and top.
โกUse of union and union all.
โกJoins in SQL inner, left, right, outer, self, full outer, cross join.
Step 2 - Level up โฌโฌ :
โกNormalization in SQL
โกAggregate, date, and string functions
โกSub-Queries
โกCTE table / with clause
โกIn-built SQL functions
โกWindow functions
โกViews
Step 3 - Practice SQL Questions on leetcode & hackerrank โ
Hope it helps :)
I have divided this SQL roadmap into 3 steps (Basics, Level Up & Practice), and it should take around 1 month to complete.
Step 1 - Basics ๐ข :
โกWhat is a Relational Database / RDBMS?
โกSQL Data Types - Varchar, text, int, number, date, float, boolean.
โกSQL commands - select, where, like, distinct, between, group by, having, order by, insert into, case when, update, truncate, delete, commit, rollback (basically all the DDL, DML, DCL, TCL commands in SQL).
โกIntegrity Constraints - Primary key, foreign key, not null, unique.
โกOperators arithmetic, logical, and comparison operations.
โกUse of distinct, order by, limit, and top.
โกUse of union and union all.
โกJoins in SQL inner, left, right, outer, self, full outer, cross join.
Step 2 - Level up โฌโฌ :
โกNormalization in SQL
โกAggregate, date, and string functions
โกSub-Queries
โกCTE table / with clause
โกIn-built SQL functions
โกWindow functions
โกViews
Step 3 - Practice SQL Questions on leetcode & hackerrank โ
Hope it helps :)
๐42โค8๐1
TOP CONCEPTS FOR INTERVIEW PREPARATION!!
๐TOP 10 SQL Concepts for Job Interview
1. Aggregate Functions (SUM/AVG)
2. Group By and Order By
3. JOINs (Inner/Left/Right)
4. Union and Union All
5. Date and Time processing
6. String processing
7. Window Functions (Partition by)
8. Subquery
9. View and Index
10. Common Table Expression (CTE)
๐TOP 10 Statistics Concepts for Job Interview
1. Sampling
2. Experiments (A/B tests)
3. Descriptive Statistics
4. p-value
5. Probability Distributions
6. t-test
7. ANOVA
8. Correlation
9. Linear Regression
10. Logistics Regression
๐TOP 10 Python Concepts for Job Interview
1. Reading data from file/table
2. Writing data to file/table
3. Data Types
4. Function
5. Data Preprocessing (numpy/pandas)
6. Data Visualisation (Matplotlib/seaborn/bokeh)
7. Machine Learning (sklearn)
8. Deep Learning (Tensorflow/Keras/PyTorch)
9. Distributed Processing (PySpark)
10. Functional and Object Oriented Programming
Hope this helps you ๐
๐TOP 10 SQL Concepts for Job Interview
1. Aggregate Functions (SUM/AVG)
2. Group By and Order By
3. JOINs (Inner/Left/Right)
4. Union and Union All
5. Date and Time processing
6. String processing
7. Window Functions (Partition by)
8. Subquery
9. View and Index
10. Common Table Expression (CTE)
๐TOP 10 Statistics Concepts for Job Interview
1. Sampling
2. Experiments (A/B tests)
3. Descriptive Statistics
4. p-value
5. Probability Distributions
6. t-test
7. ANOVA
8. Correlation
9. Linear Regression
10. Logistics Regression
๐TOP 10 Python Concepts for Job Interview
1. Reading data from file/table
2. Writing data to file/table
3. Data Types
4. Function
5. Data Preprocessing (numpy/pandas)
6. Data Visualisation (Matplotlib/seaborn/bokeh)
7. Machine Learning (sklearn)
8. Deep Learning (Tensorflow/Keras/PyTorch)
9. Distributed Processing (PySpark)
10. Functional and Object Oriented Programming
Hope this helps you ๐
๐22๐4โค2
Quick Recap of SQL Concepts
1. What is SQL?
SQL (Structured Query Language) is a standard programming language used for managing and manipulating relational databases.
2. What are the different types of SQL commands?
- Data Definition Language (DDL): Used to define the structure of database objects (CREATE, ALTER, DROP).
- Data Manipulation Language (DML): Used to manipulate data in the database (SELECT, INSERT, UPDATE, DELETE).
- Data Control Language (DCL): Used to control access and permissions on database objects (GRANT, REVOKE).
3. What is a database schema?
A database schema is a logical structure that represents the layout of the database, including tables, columns, relationships, constraints, and indexes.
4. What is a primary key?
A primary key is a unique identifier for each record in a table. It ensures that each row in the table is uniquely identified and helps maintain data integrity.
5. What is a foreign key?
A foreign key is a column or set of columns in one table that references the primary key in another table. It establishes a relationship between the two tables.
6. What is normalization in SQL?
Normalization is the process of organizing data in a database to reduce redundancy and dependency by dividing large tables into smaller tables and defining relationships between them.
7. What is an index in SQL?
An index is a data structure that improves the speed of data retrieval operations on a database table. It allows for faster searching and sorting of data based on specific columns.
8. What is a JOIN in SQL?
A JOIN is used to combine rows from two or more tables based on a related column between them. Common types of JOINs include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN.
9. What is a subquery in SQL?
A subquery is a query nested within another query. It allows you to perform complex queries by using the result of one query as input for another query.
10. What is the difference between SQL and NoSQL databases?
- SQL databases are relational databases that store data in structured tables with predefined schemas, while NoSQL databases are non-relational databases that store data in flexible, schema-less formats.
- SQL databases use SQL for querying and manipulating data, while NoSQL databases use various query languages or APIs.
- SQL databases are suitable for complex queries and transactions, while NoSQL databases are better for handling large volumes of unstructured data and scaling horizontally.
Hope it helps :)
1. What is SQL?
SQL (Structured Query Language) is a standard programming language used for managing and manipulating relational databases.
2. What are the different types of SQL commands?
- Data Definition Language (DDL): Used to define the structure of database objects (CREATE, ALTER, DROP).
- Data Manipulation Language (DML): Used to manipulate data in the database (SELECT, INSERT, UPDATE, DELETE).
- Data Control Language (DCL): Used to control access and permissions on database objects (GRANT, REVOKE).
3. What is a database schema?
A database schema is a logical structure that represents the layout of the database, including tables, columns, relationships, constraints, and indexes.
4. What is a primary key?
A primary key is a unique identifier for each record in a table. It ensures that each row in the table is uniquely identified and helps maintain data integrity.
5. What is a foreign key?
A foreign key is a column or set of columns in one table that references the primary key in another table. It establishes a relationship between the two tables.
6. What is normalization in SQL?
Normalization is the process of organizing data in a database to reduce redundancy and dependency by dividing large tables into smaller tables and defining relationships between them.
7. What is an index in SQL?
An index is a data structure that improves the speed of data retrieval operations on a database table. It allows for faster searching and sorting of data based on specific columns.
8. What is a JOIN in SQL?
A JOIN is used to combine rows from two or more tables based on a related column between them. Common types of JOINs include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN.
9. What is a subquery in SQL?
A subquery is a query nested within another query. It allows you to perform complex queries by using the result of one query as input for another query.
10. What is the difference between SQL and NoSQL databases?
- SQL databases are relational databases that store data in structured tables with predefined schemas, while NoSQL databases are non-relational databases that store data in flexible, schema-less formats.
- SQL databases use SQL for querying and manipulating data, while NoSQL databases use various query languages or APIs.
- SQL databases are suitable for complex queries and transactions, while NoSQL databases are better for handling large volumes of unstructured data and scaling horizontally.
Hope it helps :)
๐44โค2๐2๐2
How to learn SQL in 2024: Essential Topics for Beginners ๐๐
https://youtu.be/VCZxODefTIs?si=1XB44uv5DIpcJA4K
https://youtu.be/VCZxODefTIs?si=1XB44uv5DIpcJA4K
๐7
Tackle Real World Data Challenges with These SQL Key Queries...
Scenario 1: Calculating Average
Question:
You have a table Employees with columns EmployeeID, Department, and Salary. Write an SQL query to find the average salary for each department.
Answer:
Assuming the table Employees with columns EmployeeID, Department, and Salary
SELECT Department,
AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY Department;
Scenario 2: Finding Top Performers
Question:
You have a table Sales with columns SalesPersonID, SaleAmount, and SaleDate. Write an SQL query to find the top 3 salespeople with the highest total sales.
Answer:
Assuming the table Sales with columns SalesPersonID, SaleAmount, and SaleDate
SELECT SalesPersonID,
SUM(SaleAmount) AS TotalSales
FROM Sales
GROUP BY SalesPersonID
ORDER BY TotalSales DESC
LIMIT 3;
Scenario 3: Date Range Filtering
Question:
You have a table Orders with columns OrderID, OrderDate, and Amount. Write an SQL query to find the total amount of orders placed in the last 30 days.
Answer:
Assuming the table Orders with columns OrderID, OrderDate, and Amount
SELECT SUM(Amount) AS TotalAmount
FROM Orders
WHERE OrderDate >= CURDATE() - INTERVAL 30 DAY;
Hope it helps :)
Scenario 1: Calculating Average
Question:
You have a table Employees with columns EmployeeID, Department, and Salary. Write an SQL query to find the average salary for each department.
Answer:
Assuming the table Employees with columns EmployeeID, Department, and Salary
SELECT Department,
AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY Department;
Scenario 2: Finding Top Performers
Question:
You have a table Sales with columns SalesPersonID, SaleAmount, and SaleDate. Write an SQL query to find the top 3 salespeople with the highest total sales.
Answer:
Assuming the table Sales with columns SalesPersonID, SaleAmount, and SaleDate
SELECT SalesPersonID,
SUM(SaleAmount) AS TotalSales
FROM Sales
GROUP BY SalesPersonID
ORDER BY TotalSales DESC
LIMIT 3;
Scenario 3: Date Range Filtering
Question:
You have a table Orders with columns OrderID, OrderDate, and Amount. Write an SQL query to find the total amount of orders placed in the last 30 days.
Answer:
Assuming the table Orders with columns OrderID, OrderDate, and Amount
SELECT SUM(Amount) AS TotalAmount
FROM Orders
WHERE OrderDate >= CURDATE() - INTERVAL 30 DAY;
Hope it helps :)
๐29โค6๐3
Some frequently Asked SQL Interview Questions with Answers in data analyst interviews:
1. Write a SQL query to find the average purchase amount for each customer. Assume you have two tables: Customers (CustomerID, Name) and Orders (OrderID, CustomerID, Amount).
SELECT c.CustomerID, c. Name, AVG(o.Amount) AS AveragePurchase
FROM Customers c
JOIN Orders o ON c.CustomerID = o.CustomerID
GROUP BY c.CustomerID, c. Name;
2. Write a query to find the employee with the minimum salary in each department from a table Employees with columns EmployeeID, Name, DepartmentID, and Salary.
SELECT e1.DepartmentID, e1.EmployeeID, e1 .Name, e1.Salary
FROM Employees e1
WHERE Salary = (SELECT MIN(Salary) FROM Employees e2 WHERE e2.DepartmentID = e1.DepartmentID);
3. Write a SQL query to find all products that have never been sold. Assume you have a table Products (ProductID, ProductName) and a table Sales (SaleID, ProductID, Quantity).
SELECT p.ProductID, p.ProductName
FROM Products p
LEFT JOIN Sales s ON p.ProductID = s.ProductID
WHERE s.ProductID IS NULL;
4. Given a table Orders with columns OrderID, CustomerID, OrderDate, and a table OrderItems with columns OrderID, ItemID, Quantity, write a query to find the customer with the highest total order quantity.
SELECT o.CustomerID, SUM(oi.Quantity) AS TotalQuantity
FROM Orders o
JOIN OrderItems oi ON o.OrderID = oi.OrderID
GROUP BY o.CustomerID
ORDER BY TotalQuantity DESC
LIMIT 1;
5. Write a SQL query to find the earliest order date for each customer from a table Orders (OrderID, CustomerID, OrderDate).
SELECT CustomerID, MIN(OrderDate) AS EarliestOrderDate
FROM Orders
GROUP BY CustomerID;
6. Given a table Employees with columns EmployeeID, Name, ManagerID, write a query to find the number of direct reports for each manager.
SELECT ManagerID, COUNT(*) AS NumberOfReports
FROM Employees
WHERE ManagerID IS NOT NULL
GROUP BY ManagerID;
7. Given a table Customers with columns CustomerID, Name, JoinDate, and a table Orders with columns OrderID, CustomerID, OrderDate, write a query to find customers who placed their first order within the last 30 days.
SELECT c.CustomerID, c. Name
FROM Customers c
JOIN Orders o ON c.CustomerID = o.CustomerID
WHERE o.OrderDate = (SELECT MIN(o2.OrderDate) FROM Orders o2 WHERE o2.CustomerID = c.CustomerID)
AND o.OrderDate >= CURRENT_DATE - INTERVAL '30 day';
Hope it helps :)
1. Write a SQL query to find the average purchase amount for each customer. Assume you have two tables: Customers (CustomerID, Name) and Orders (OrderID, CustomerID, Amount).
SELECT c.CustomerID, c. Name, AVG(o.Amount) AS AveragePurchase
FROM Customers c
JOIN Orders o ON c.CustomerID = o.CustomerID
GROUP BY c.CustomerID, c. Name;
2. Write a query to find the employee with the minimum salary in each department from a table Employees with columns EmployeeID, Name, DepartmentID, and Salary.
SELECT e1.DepartmentID, e1.EmployeeID, e1 .Name, e1.Salary
FROM Employees e1
WHERE Salary = (SELECT MIN(Salary) FROM Employees e2 WHERE e2.DepartmentID = e1.DepartmentID);
3. Write a SQL query to find all products that have never been sold. Assume you have a table Products (ProductID, ProductName) and a table Sales (SaleID, ProductID, Quantity).
SELECT p.ProductID, p.ProductName
FROM Products p
LEFT JOIN Sales s ON p.ProductID = s.ProductID
WHERE s.ProductID IS NULL;
4. Given a table Orders with columns OrderID, CustomerID, OrderDate, and a table OrderItems with columns OrderID, ItemID, Quantity, write a query to find the customer with the highest total order quantity.
SELECT o.CustomerID, SUM(oi.Quantity) AS TotalQuantity
FROM Orders o
JOIN OrderItems oi ON o.OrderID = oi.OrderID
GROUP BY o.CustomerID
ORDER BY TotalQuantity DESC
LIMIT 1;
5. Write a SQL query to find the earliest order date for each customer from a table Orders (OrderID, CustomerID, OrderDate).
SELECT CustomerID, MIN(OrderDate) AS EarliestOrderDate
FROM Orders
GROUP BY CustomerID;
6. Given a table Employees with columns EmployeeID, Name, ManagerID, write a query to find the number of direct reports for each manager.
SELECT ManagerID, COUNT(*) AS NumberOfReports
FROM Employees
WHERE ManagerID IS NOT NULL
GROUP BY ManagerID;
7. Given a table Customers with columns CustomerID, Name, JoinDate, and a table Orders with columns OrderID, CustomerID, OrderDate, write a query to find customers who placed their first order within the last 30 days.
SELECT c.CustomerID, c. Name
FROM Customers c
JOIN Orders o ON c.CustomerID = o.CustomerID
WHERE o.OrderDate = (SELECT MIN(o2.OrderDate) FROM Orders o2 WHERE o2.CustomerID = c.CustomerID)
AND o.OrderDate >= CURRENT_DATE - INTERVAL '30 day';
Hope it helps :)
๐14๐คฃ2
Many people pay too much to learn SQL, but my mission is to break down barriers. I have shared complete learning series to learn SQL from scratch.
Here are the links to the SQL series
Complete SQL Topics for Data Analyst: https://t.me/sqlspecialist/523
Part-1: https://t.me/sqlspecialist/524
Part-2: https://t.me/sqlspecialist/525
Part-3: https://t.me/sqlspecialist/526
Part-4: https://t.me/sqlspecialist/527
Part-5: https://t.me/sqlspecialist/529
Part-6: https://t.me/sqlspecialist/534
Part-7: https://t.me/sqlspecialist/534
Part-8: https://t.me/sqlspecialist/536
Part-9: https://t.me/sqlspecialist/537
Part-10: https://t.me/sqlspecialist/539
Part-11: https://t.me/sqlspecialist/540
Part-12:
https://t.me/sqlspecialist/541
Part-13: https://t.me/sqlspecialist/542
Part-14: https://t.me/sqlspecialist/544
Part-15: https://t.me/sqlspecialist/545
Part-16: https://t.me/sqlspecialist/546
Part-17: https://t.me/sqlspecialist/549
Part-18: https://t.me/sqlspecialist/552
Part-19: https://t.me/sqlspecialist/555
Part-20: https://t.me/sqlspecialist/556
I saw a lot of big influencers copy pasting my content after removing the credits. It's absolutely fine for me as more people are getting free education because of my content.
But I will really appreciate if you share credits for the time and efforts I put in to create such valuable content. I hope you can understand.
Complete Python Topics for Data Analysts: https://t.me/sqlspecialist/548
Complete Excel Topics for Data Analysts: https://t.me/sqlspecialist/547
I'll continue with learning series on Python, Power BI, Excel & Tableau.
Thanks to all who support our channel and share the content with proper credits. You guys are really amazing.
Hope it helps :)
Here are the links to the SQL series
Complete SQL Topics for Data Analyst: https://t.me/sqlspecialist/523
Part-1: https://t.me/sqlspecialist/524
Part-2: https://t.me/sqlspecialist/525
Part-3: https://t.me/sqlspecialist/526
Part-4: https://t.me/sqlspecialist/527
Part-5: https://t.me/sqlspecialist/529
Part-6: https://t.me/sqlspecialist/534
Part-7: https://t.me/sqlspecialist/534
Part-8: https://t.me/sqlspecialist/536
Part-9: https://t.me/sqlspecialist/537
Part-10: https://t.me/sqlspecialist/539
Part-11: https://t.me/sqlspecialist/540
Part-12:
https://t.me/sqlspecialist/541
Part-13: https://t.me/sqlspecialist/542
Part-14: https://t.me/sqlspecialist/544
Part-15: https://t.me/sqlspecialist/545
Part-16: https://t.me/sqlspecialist/546
Part-17: https://t.me/sqlspecialist/549
Part-18: https://t.me/sqlspecialist/552
Part-19: https://t.me/sqlspecialist/555
Part-20: https://t.me/sqlspecialist/556
I saw a lot of big influencers copy pasting my content after removing the credits. It's absolutely fine for me as more people are getting free education because of my content.
But I will really appreciate if you share credits for the time and efforts I put in to create such valuable content. I hope you can understand.
Complete Python Topics for Data Analysts: https://t.me/sqlspecialist/548
Complete Excel Topics for Data Analysts: https://t.me/sqlspecialist/547
I'll continue with learning series on Python, Power BI, Excel & Tableau.
Thanks to all who support our channel and share the content with proper credits. You guys are really amazing.
Hope it helps :)
๐20โค4๐4๐2๐ค1๐คฃ1
Complete topics & subtopics of SQL for Data Analyst role:-
1. SQL Fundamentals
A. SQL Basics
โข SQL Keywords and Syntax
โข Data Types (Numeric, String, Date/Time, etc.)
โข Operators (Arithmetic, Comparison, Logical)
B. Core SQL Statements
โข SELECT
โข INSERT
โข UPDATE
โข DELETE
2. Database Design and Schema
A. Data Definition Language (DDL)
โข CREATE TABLE
โข ALTER TABLE
โข DROP TABLE
โข TRUNCATE TABLE
B. Data Constraints
โข Primary Key
โข Foreign Key
โข Unique
โข NOT NULL
โข CHECK
3. Querying and Data Manipulation
A. Data Manipulation Language (DML)
โข SELECT Clauses (SELECT, FROM, WHERE)
โข Sorting and Filtering (ORDER BY, GROUP BY, HAVING)
โข JOIN Operations (INNER, LEFT, RIGHT, FULL OUTER, SELF, CROSS)
โข INSERT, UPDATE, DELETE Operations
B. Aggregate Functions and Grouping
โข Functions (SUM, AVG, COUNT, MIN, MAX)
โข GROUP BY and HAVING Clauses
4. Advanced Querying Techniques
A. Joins and Subqueries
โข Types of Joins and Their Use Cases
โข Subqueries (Scalar, Column, Row, Table)
โข Nested and Correlated Subqueries
B. Advanced SQL Functions
โข String Functions (CONCAT, LENGTH, SUBSTRING, REPLACE, UPPER, LOWER)
โข Date/Time Functions (DATE, TIME, TIMESTAMP, DATEPART, DATEADD)
โข Numeric Functions (ROUND, CEILING, FLOOR, ABS, MOD)
โข Conditional Functions (CASE, COALESCE, NULLIF)
5. Views and Indexes
A. Views
โข Creating and Managing Views
โข Modifying and Dropping Views
B. Indexes
โข Types of Indexes (Single Column, Composite)
โข Creating and Using Indexes for Optimization
6. Security and Data Integrity
A. Data Integrity
โข Referential and Entity Integrity
โข Enforcing Data Constraints
B. Security Management
โข GRANT and REVOKE Permissions
โข Best Practices for Database Security
7. Stored procedure and functions
A. Stored Procedures
โข Creating, Modifying, and Executing Stored Procedures
โข Benefits and Use Cases
B. Functions
โข User-Defined Functions
โข Using Functions in Queries
8. Performance Optimization
A. Query Optimization Techniques
โข Index Usage
โข Optimizing Joins and Subqueries
โข Execution Plans and Query Analysis
B. Performance Tuning Best Practices
โข Avoiding Common Pitfalls
โข Regular Maintenance and Updates
9. Advanced SQL Features
A. Complex Query Techniques
โข Recursive Queries
โข Pivot and Unpivot Operations
โข Window Functions (ROW_NUMBER, RANK, DENSE_RANK, LEAD, LAG)
B. Common Table Expressions (CTEs) and Dynamic SQL
โข Using CTEs for Improved Readability
โข Implementing Dynamic SQL for Flexible Queries
10. Practical Applications and Case Studies
โข Real-World SQL Scenarios
โข Data Analysis Case Studies
โข Problem-Solving with SQL
Hope it helps :)
1. SQL Fundamentals
A. SQL Basics
โข SQL Keywords and Syntax
โข Data Types (Numeric, String, Date/Time, etc.)
โข Operators (Arithmetic, Comparison, Logical)
B. Core SQL Statements
โข SELECT
โข INSERT
โข UPDATE
โข DELETE
2. Database Design and Schema
A. Data Definition Language (DDL)
โข CREATE TABLE
โข ALTER TABLE
โข DROP TABLE
โข TRUNCATE TABLE
B. Data Constraints
โข Primary Key
โข Foreign Key
โข Unique
โข NOT NULL
โข CHECK
3. Querying and Data Manipulation
A. Data Manipulation Language (DML)
โข SELECT Clauses (SELECT, FROM, WHERE)
โข Sorting and Filtering (ORDER BY, GROUP BY, HAVING)
โข JOIN Operations (INNER, LEFT, RIGHT, FULL OUTER, SELF, CROSS)
โข INSERT, UPDATE, DELETE Operations
B. Aggregate Functions and Grouping
โข Functions (SUM, AVG, COUNT, MIN, MAX)
โข GROUP BY and HAVING Clauses
4. Advanced Querying Techniques
A. Joins and Subqueries
โข Types of Joins and Their Use Cases
โข Subqueries (Scalar, Column, Row, Table)
โข Nested and Correlated Subqueries
B. Advanced SQL Functions
โข String Functions (CONCAT, LENGTH, SUBSTRING, REPLACE, UPPER, LOWER)
โข Date/Time Functions (DATE, TIME, TIMESTAMP, DATEPART, DATEADD)
โข Numeric Functions (ROUND, CEILING, FLOOR, ABS, MOD)
โข Conditional Functions (CASE, COALESCE, NULLIF)
5. Views and Indexes
A. Views
โข Creating and Managing Views
โข Modifying and Dropping Views
B. Indexes
โข Types of Indexes (Single Column, Composite)
โข Creating and Using Indexes for Optimization
6. Security and Data Integrity
A. Data Integrity
โข Referential and Entity Integrity
โข Enforcing Data Constraints
B. Security Management
โข GRANT and REVOKE Permissions
โข Best Practices for Database Security
7. Stored procedure and functions
A. Stored Procedures
โข Creating, Modifying, and Executing Stored Procedures
โข Benefits and Use Cases
B. Functions
โข User-Defined Functions
โข Using Functions in Queries
8. Performance Optimization
A. Query Optimization Techniques
โข Index Usage
โข Optimizing Joins and Subqueries
โข Execution Plans and Query Analysis
B. Performance Tuning Best Practices
โข Avoiding Common Pitfalls
โข Regular Maintenance and Updates
9. Advanced SQL Features
A. Complex Query Techniques
โข Recursive Queries
โข Pivot and Unpivot Operations
โข Window Functions (ROW_NUMBER, RANK, DENSE_RANK, LEAD, LAG)
B. Common Table Expressions (CTEs) and Dynamic SQL
โข Using CTEs for Improved Readability
โข Implementing Dynamic SQL for Flexible Queries
10. Practical Applications and Case Studies
โข Real-World SQL Scenarios
โข Data Analysis Case Studies
โข Problem-Solving with SQL
Hope it helps :)
๐15โค4
For a data analytics interview, focusing on key SQL topics can be crucial. Here's a list of last-minute SQL topics to revise:
1. SQL Basics:
โข SELECT statements: Syntax, SELECT DISTINCT
โข WHERE clause: Conditions and operators (>, <, =, LIKE, IN, BETWEEN)
โข ORDER BY clause: Sorting results
โข LIMIT clause: Limiting the number of rows returned
2. Joins:
โข INNER JOIN
โข LEFT (OUTER) JOIN
โข RIGHT (OUTER) JOIN
โข FULL (OUTER) JOIN
โข CROSS JOIN
โข Understanding join conditions and scenarios for each type of join
3. Aggregation and Grouping:
โข GROUP BY clause
โข HAVING clause: Filtering grouped results
โข Aggregate functions: COUNT, SUM, AVG, MIN, MAX
4. Subqueries:
โข Nested subqueries: Using subqueries in SELECT, FROM, WHERE, and HAVING clauses
โข Correlated subqueries
5. Common Table Expressions (CTEs):
โข Syntax and use cases for CTEs (WITH clause)
6. Window Functions:
โข ROW_NUMBER()
โข RANK()
โข DENSE_RANK()
โข LEAD() and LAG()
โข PARTITION BY clause
7. Data Manipulation:
โข INSERT, UPDATE, DELETE statements
โข Understanding transaction control with COMMIT and ROLLBACK
8. Data Definition:
โข CREATE TABLE
โข ALTER TABLE
โข DROP TABLE
โข Constraints: PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL
9. Indexing:
โข Purpose and types of indexes
โข How indexing affects query performance
10. Performance Optimization:
โข Understanding query execution plans
โข Identifying and resolving common performance issues
11. SQL Functions:
โข String functions: CONCAT, SUBSTRING, LENGTH
โข Date functions: DATEADD, DATEDIFF, GETDATE
โข Mathematical functions: ROUND, CEILING, FLOOR
12. Stored Procedures and Triggers:
โข Basics of writing and using stored procedures
โข Basics of writing and using triggers
13. ETL (Extract, Transform, Load):
โข Understanding the process and SQL's role in ETL operations
14. Advanced Topics (if time permits):
โข Understanding complex data types (JSON, XML)
โข Working with large datasets and big data considerations
Hope it helps :)
1. SQL Basics:
โข SELECT statements: Syntax, SELECT DISTINCT
โข WHERE clause: Conditions and operators (>, <, =, LIKE, IN, BETWEEN)
โข ORDER BY clause: Sorting results
โข LIMIT clause: Limiting the number of rows returned
2. Joins:
โข INNER JOIN
โข LEFT (OUTER) JOIN
โข RIGHT (OUTER) JOIN
โข FULL (OUTER) JOIN
โข CROSS JOIN
โข Understanding join conditions and scenarios for each type of join
3. Aggregation and Grouping:
โข GROUP BY clause
โข HAVING clause: Filtering grouped results
โข Aggregate functions: COUNT, SUM, AVG, MIN, MAX
4. Subqueries:
โข Nested subqueries: Using subqueries in SELECT, FROM, WHERE, and HAVING clauses
โข Correlated subqueries
5. Common Table Expressions (CTEs):
โข Syntax and use cases for CTEs (WITH clause)
6. Window Functions:
โข ROW_NUMBER()
โข RANK()
โข DENSE_RANK()
โข LEAD() and LAG()
โข PARTITION BY clause
7. Data Manipulation:
โข INSERT, UPDATE, DELETE statements
โข Understanding transaction control with COMMIT and ROLLBACK
8. Data Definition:
โข CREATE TABLE
โข ALTER TABLE
โข DROP TABLE
โข Constraints: PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL
9. Indexing:
โข Purpose and types of indexes
โข How indexing affects query performance
10. Performance Optimization:
โข Understanding query execution plans
โข Identifying and resolving common performance issues
11. SQL Functions:
โข String functions: CONCAT, SUBSTRING, LENGTH
โข Date functions: DATEADD, DATEDIFF, GETDATE
โข Mathematical functions: ROUND, CEILING, FLOOR
12. Stored Procedures and Triggers:
โข Basics of writing and using stored procedures
โข Basics of writing and using triggers
13. ETL (Extract, Transform, Load):
โข Understanding the process and SQL's role in ETL operations
14. Advanced Topics (if time permits):
โข Understanding complex data types (JSON, XML)
โข Working with large datasets and big data considerations
Hope it helps :)
๐18โค2
SQL books wonโt teach you this.
Natural Keys vs. Autoincrement IDs vs. Public IDs. (or maybe all together)
๐ก๐ฎ๐๐๐ฟ๐ฎ๐น ๐๐ฒ๐๐
Natural keys carry intrinsic meaning because they are part of the domain.
They are directly related to the data, making them intuitive and easy to understand. Examples include email addresses or employee IDs.
The problem is that they are usually not good for performance, but they can also be a security risk if you expose them.
๐๐๐๐ผ๐ถ๐ป๐ฐ๐ฟ๐ฒ๐บ๐ฒ๐ป๐ ๐๐๐
Autoincrement IDs automatically generate unique integers to identify rows within a table.
They are often used as primary keys.
Simple integers are fast for the database to index and query. They provide optimal performance.
However, they are vulnerable to enumeration attacks since predicting the next or previous record is easy.
๐ฃ๐๐ฏ๐น๐ถ๐ฐ ๐๐๐ (๐จ๐จ๐๐๐)
UUIDs (Universally Unique Identifiers) are 128-bit identifiers used to uniquely identify information without relying on a centralized authority.
They are difficult to guess, making them suitable for public exposure in APIs.
The problem is they are larger and more complex than integers. This can impact performance, particularly in indexing and storage.
๐๐ถ๐ป๐ฑ๐ถ๐ป๐ด ๐๐ต๐ฒ ๐ฆ๐๐ฒ๐ฒ๐ ๐ฆ๐ฝ๐ผ๐: ๐ ๐ ๐ถ๐ ๐ฒ๐ฑ ๐๐ฝ๐ฝ๐ฟ๐ผ๐ฎ๐ฐ๐ต
Combining different types of keys can offer a balanced solution:
โข InternalID: Used for internal operations and relationships between tables.
โข PublicID: Used in API responses and endpoints to securely reference user records.
โข Email (Natural Key): Used to ensure unique identification of users within the business logic.
The mixed approach keeps your system fast, secure, and easy to understand.
Like this post if you need more ๐โค๏ธ
Hope it helps :)
Natural Keys vs. Autoincrement IDs vs. Public IDs. (or maybe all together)
๐ก๐ฎ๐๐๐ฟ๐ฎ๐น ๐๐ฒ๐๐
Natural keys carry intrinsic meaning because they are part of the domain.
They are directly related to the data, making them intuitive and easy to understand. Examples include email addresses or employee IDs.
The problem is that they are usually not good for performance, but they can also be a security risk if you expose them.
๐๐๐๐ผ๐ถ๐ป๐ฐ๐ฟ๐ฒ๐บ๐ฒ๐ป๐ ๐๐๐
Autoincrement IDs automatically generate unique integers to identify rows within a table.
They are often used as primary keys.
Simple integers are fast for the database to index and query. They provide optimal performance.
However, they are vulnerable to enumeration attacks since predicting the next or previous record is easy.
๐ฃ๐๐ฏ๐น๐ถ๐ฐ ๐๐๐ (๐จ๐จ๐๐๐)
UUIDs (Universally Unique Identifiers) are 128-bit identifiers used to uniquely identify information without relying on a centralized authority.
They are difficult to guess, making them suitable for public exposure in APIs.
The problem is they are larger and more complex than integers. This can impact performance, particularly in indexing and storage.
๐๐ถ๐ป๐ฑ๐ถ๐ป๐ด ๐๐ต๐ฒ ๐ฆ๐๐ฒ๐ฒ๐ ๐ฆ๐ฝ๐ผ๐: ๐ ๐ ๐ถ๐ ๐ฒ๐ฑ ๐๐ฝ๐ฝ๐ฟ๐ผ๐ฎ๐ฐ๐ต
Combining different types of keys can offer a balanced solution:
โข InternalID: Used for internal operations and relationships between tables.
โข PublicID: Used in API responses and endpoints to securely reference user records.
โข Email (Natural Key): Used to ensure unique identification of users within the business logic.
The mixed approach keeps your system fast, secure, and easy to understand.
Like this post if you need more ๐โค๏ธ
Hope it helps :)
๐7โค5
Most Asked SQL Interview Questions at MAANG Companies๐ฅ๐ฅ
Preparing for an SQL Interview at MAANG Companies? Here are some crucial SQL Questions you should be ready to tackle:
1. How do you retrieve all columns from a table?
SELECT * FROM table_name;
2. What SQL statement is used to filter records?
SELECT * FROM table_name
WHERE condition;
The WHERE clause is used to filter records based on a specified condition.
3. How can you join multiple tables? Describe different types of JOINs.
SELECT columns
FROM table1
JOIN table2 ON table1.column = table2.column
JOIN table3 ON table2.column = table3.column;
Types of JOINs:
1. INNER JOIN: Returns records with matching values in both tables
SELECT * FROM table1
INNER JOIN table2 ON table1.column = table2.column;
2. LEFT JOIN: Returns all records from the left table & matched records from the right table. Unmatched records will have NULL values.
SELECT * FROM table1
LEFT JOIN table2 ON table1.column = table2.column;
3. RIGHT JOIN: Returns all records from the right table & matched records from the left table. Unmatched records will have NULL values.
SELECT * FROM table1
RIGHT JOIN table2 ON table1.column = table2.column;
4. FULL JOIN: Returns records when there is a match in either left or right table. Unmatched records will have NULL values.
SELECT * FROM table1
FULL JOIN table2 ON table1.column = table2.column;
4. What is the difference between WHERE & HAVING clauses?
WHERE: Filters records before any groupings are made.
SELECT * FROM table_name
WHERE condition;
HAVING: Filters records after groupings are made.
SELECT column, COUNT(*)
FROM table_name
GROUP BY column
HAVING COUNT(*) > value;
5. How do you calculate average, sum, minimum & maximum values in a column?
Average: SELECT AVG(column_name) FROM table_name;
Sum: SELECT SUM(column_name) FROM table_name;
Minimum: SELECT MIN(column_name) FROM table_name;
Maximum: SELECT MAX(column_name) FROM table_name;
Hope it helps :)
Preparing for an SQL Interview at MAANG Companies? Here are some crucial SQL Questions you should be ready to tackle:
1. How do you retrieve all columns from a table?
SELECT * FROM table_name;
2. What SQL statement is used to filter records?
SELECT * FROM table_name
WHERE condition;
The WHERE clause is used to filter records based on a specified condition.
3. How can you join multiple tables? Describe different types of JOINs.
SELECT columns
FROM table1
JOIN table2 ON table1.column = table2.column
JOIN table3 ON table2.column = table3.column;
Types of JOINs:
1. INNER JOIN: Returns records with matching values in both tables
SELECT * FROM table1
INNER JOIN table2 ON table1.column = table2.column;
2. LEFT JOIN: Returns all records from the left table & matched records from the right table. Unmatched records will have NULL values.
SELECT * FROM table1
LEFT JOIN table2 ON table1.column = table2.column;
3. RIGHT JOIN: Returns all records from the right table & matched records from the left table. Unmatched records will have NULL values.
SELECT * FROM table1
RIGHT JOIN table2 ON table1.column = table2.column;
4. FULL JOIN: Returns records when there is a match in either left or right table. Unmatched records will have NULL values.
SELECT * FROM table1
FULL JOIN table2 ON table1.column = table2.column;
4. What is the difference between WHERE & HAVING clauses?
WHERE: Filters records before any groupings are made.
SELECT * FROM table_name
WHERE condition;
HAVING: Filters records after groupings are made.
SELECT column, COUNT(*)
FROM table_name
GROUP BY column
HAVING COUNT(*) > value;
5. How do you calculate average, sum, minimum & maximum values in a column?
Average: SELECT AVG(column_name) FROM table_name;
Sum: SELECT SUM(column_name) FROM table_name;
Minimum: SELECT MIN(column_name) FROM table_name;
Maximum: SELECT MAX(column_name) FROM table_name;
Hope it helps :)
๐26โค5๐1
Why SQL Still Rules the Data World?
SQL + Relational Databases = Structured Data Management
SQL + Joins = Seamless Data Integration
SQL + Aggregations = Powerful Data Summarization
SQL + Subqueries = Complex Data Retrieval
SQL + Indexing = Faster Query Performance
SQL + Transactions = Reliable Data Integrity
SQL + Views = Simplified Data Access
SQL + Stored Procedures = Efficient Data Operations
SQL + Triggers = Automated Actions Based on Data Changes
SQL + Constraints = Data Validation and Integrity
SQL + Normalization = Eliminate Redundancy
SQL + Data Warehousing = Scalable Data Storage Solutions
SQL + Data Lakes = Manage Vast Amounts of Raw Data
SQL + ETL Processes = Efficient Data Transformation
SQL + Backup and Recovery = Secure Data Management
SQL + Big Data Integration = Bridging SQL and NoSQL
SQL + Reporting Tools = Generating Insightful Reports
SQL + BI Tools = Business Intelligence Integration
SQL + Analytics = Deep Data Insights
SQL remains unbeatable with its ability to manage, query, and analyze data efficiently.
Hope it helps :)
SQL + Relational Databases = Structured Data Management
SQL + Joins = Seamless Data Integration
SQL + Aggregations = Powerful Data Summarization
SQL + Subqueries = Complex Data Retrieval
SQL + Indexing = Faster Query Performance
SQL + Transactions = Reliable Data Integrity
SQL + Views = Simplified Data Access
SQL + Stored Procedures = Efficient Data Operations
SQL + Triggers = Automated Actions Based on Data Changes
SQL + Constraints = Data Validation and Integrity
SQL + Normalization = Eliminate Redundancy
SQL + Data Warehousing = Scalable Data Storage Solutions
SQL + Data Lakes = Manage Vast Amounts of Raw Data
SQL + ETL Processes = Efficient Data Transformation
SQL + Backup and Recovery = Secure Data Management
SQL + Big Data Integration = Bridging SQL and NoSQL
SQL + Reporting Tools = Generating Insightful Reports
SQL + BI Tools = Business Intelligence Integration
SQL + Analytics = Deep Data Insights
SQL remains unbeatable with its ability to manage, query, and analyze data efficiently.
Hope it helps :)
๐11โค8
SQL best practices:
โ Use EXISTS in place of IN wherever possible
โ Use table aliases with columns when you are joining multiple tables
โ Use GROUP BY instead of DISTINCT.
โ Add useful comments wherever you write complex logic and avoid too many comments.
โ Use joins instead of subqueries when possible for better performance.
โ Use WHERE instead of HAVING to define filters on non-aggregate fields
โ Avoid wildcards at beginning of predicates (something like '%abc' will cause full table scan to get the results)
โ Considering cardinality within GROUP BY can make it faster (try to consider unique column first in group by list)
โ Write SQL keywords in capital letters.
โ Never use select *, always mention list of columns in select clause.
โ Create CTEs instead of multiple sub queries , it will make your query easy to read.
โ Join tables using JOIN keywords instead of writing join condition in where clause for better readability.
โ Never use order by in sub queries , It will unnecessary increase runtime.
โ If you know there are no duplicates in 2 tables, use UNION ALL instead of UNION for better performance
โ Always start WHERE clause with 1 = 1.This has the advantage of easily commenting out conditions during debugging a query.
โ Taking care of NULL values before using equality or comparisons operators. Applying window functions. Filtering the query before joining and having clause.
โ Make sure the JOIN conditions among two table Join are either keys or Indexed attribute.
Hope it helps :)
โ Use EXISTS in place of IN wherever possible
โ Use table aliases with columns when you are joining multiple tables
โ Use GROUP BY instead of DISTINCT.
โ Add useful comments wherever you write complex logic and avoid too many comments.
โ Use joins instead of subqueries when possible for better performance.
โ Use WHERE instead of HAVING to define filters on non-aggregate fields
โ Avoid wildcards at beginning of predicates (something like '%abc' will cause full table scan to get the results)
โ Considering cardinality within GROUP BY can make it faster (try to consider unique column first in group by list)
โ Write SQL keywords in capital letters.
โ Never use select *, always mention list of columns in select clause.
โ Create CTEs instead of multiple sub queries , it will make your query easy to read.
โ Join tables using JOIN keywords instead of writing join condition in where clause for better readability.
โ Never use order by in sub queries , It will unnecessary increase runtime.
โ If you know there are no duplicates in 2 tables, use UNION ALL instead of UNION for better performance
โ Always start WHERE clause with 1 = 1.This has the advantage of easily commenting out conditions during debugging a query.
โ Taking care of NULL values before using equality or comparisons operators. Applying window functions. Filtering the query before joining and having clause.
โ Make sure the JOIN conditions among two table Join are either keys or Indexed attribute.
Hope it helps :)
๐20โค4