SQL Queries for Data Analyst
As a data analyst or data scientist, knowing the right SQL queries can help you get the data you need from your database tables quickly and easily. Whether you’re a new user or a pro, you need to know how to use the basic SQL queries to analyze and change data. In this blog post, we’ll look at ten basic SQL queries that every data analyst should know, ten advanced SQL queries that every data scientist should know, and ten intermediate SQL queries that build on the basics and offer more advanced ways to filter and change data. By the end of this post, you’ll have a good grasp of SQL, which will help you analyze and change your data in a useful way.
Here are some great links that will help you:
Practice SQL Queries on our Website
Must Know SQL Queries for Data Analyst
These are the basics that we allow you to retrieve and manipulate data from your database without any issues. However, you should take a few more leaps into the SQL space to master the art of data analysis using SQL. Lets look at some basic SQL queries.
SELECT: This query is used to retrieve data from one or more database tables. It is the most basic and essential SQL query.
SELECT * FROM my_table;
WHERE: This query is used to filter records based on specified criteria.
SELECT * FROM my_table WHERE column1 = 'value';
GROUP BY: This query is used to group records based on one or more columns.
SELECT column1, COUNT(*) FROM my_table GROUP BY column1;
HAVING: This query is used to filter records based on the result of an aggregate function.
SELECT column1, COUNT(*) FROM my_table GROUP BY column1 HAVING COUNT(*) > 10;
ORDER BY: This query is used to sort records in ascending or descending order based on one or more columns.
SELECT * FROM my_table ORDER BY column1 DESC;
JOIN: This query is used to combine two or more tables based on a common column.
SELECT * FROM table1 JOIN table2 ON table1.column1 = table2.column2;
UNION: This query is used to combine the results of two or more SELECT statements.
SELECT column1 FROM table1 UNION SELECT column1 FROM table2;
CASE: This query is used to perform conditional logic within a SQL statement.
SELECT column1,CASE WHEN column2 > 10 THEN 'High' ELSE 'Low' END AS column3 FROM my_table;
Intermediate Level SQL Queries for Data Analyst
BETWEEN: This query allows you to filter records based on a range of values.
SELECT * FROM my_table WHERE column1 BETWEEN 10 AND 20;
LIKE: This query allows you to filter records based on a pattern in a string.
SELECT * FROM my_table WHERE column1 LIKE '%pattern%';
NULL: This query allows you to filter records based on null values.
SELECT * FROM my_table WHERE column1 IS NULL;
IN: This query allows you to filter records based on a list of values.
SELECT * FROM my_table WHERE column1 IN ('value1', 'value2', 'value3');
LIMIT: This query allows you to limit the number of records returned by a SELECT statement.
SELECT * FROM my_table LIMIT 10;
OFFSET: This query allows you to skip a specified number of records before returning the rest.
SELECT * FROM my_table LIMIT 10 OFFSET 20;
- COALESCE: This query allows you to return the first non-null value in a list of values.
SELECT COALESCE(column1, column2, column3) AS column4 FROM my_table;
DATE_FORMAT: This query allows you to format a date or time value as a string.
SELECT DATE_FORMAT(column1, '%Y-%m-%d') AS formatted_date FROM my_table;
Slightly Advanced SQL Queries for Data Analyst
Window Functions: This query allows you to perform calculations across a group of rows that are related to the current row.
SELECT column1, AVG(column2) OVER (PARTITION BY column3) FROM my_table;
Subqueries: This query allows you to nest a SELECT statement inside another SELECT statement.
SELECT column1 FROM my_table WHERE column2 IN (SELECT column2 FROM other_table WHERE column3 = 'value');
Pivoting: This query allows you to convert rows into columns, or columns into rows.
SELECT column1, MAX(CASE WHEN column2 = 'value1' THEN column3 END) AS value1, MAX(CASE WHEN column2 = 'value2' THEN column3 END) AS value2 FROM my_table GROUP BY column1;
Date Functions: This query allows you to manipulate dates and times.
SELECT DATE_TRUNC('week', column1) AS week, SUM(column2) FROM my_table GROUP BY week;
Ranking Functions: This query allows you to assign a rank to each row based on a specified order.
SELECT column1, column2, RANK() OVER (PARTITION BY column1 ORDER BY column2 DESC) FROM my_table;
Full-Text Search: This query allows you to search for text within a column that contains text data.
SELECT * FROM my_table WHERE MATCH(column1) AGAINST('search term');
Common Table Expression (CTE): One line is simply a SELECT statement that is defined using the WITH keyword and a name for the CTE.
WITH my_cte AS (SELECT column1, column2 FROM my_table) SELECT * FROM my_cte WHERE column1 = ‘value’;