Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

Top 10 SQL Queries a Data Scientist should know
Latest

Top 10 SQL Queries a Data Scientist should know

Last Updated on September 28, 2022 by Editorial Team

Author(s): Saurabh Saxena

Originally published on Towards AI the World’s Leading AI and Technology News and Media Company. If you are building an AI-related product or service, we invite you to consider becoming an AI sponsor. At Towards AI, we help scale AI and technology startups. Let us help you unleash your technology to the masses.

Top 10 SQL Queries a Data Scientist ShouldΒ Know

Photo by Campaign Creators onΒ Unsplash

Structured Query Language or SQL is a query-based universal language to read, write and manage databases. In any Machine Learning pipeline, whether it is data, metadata, or logs, SQL is widely used in all operations.

When you are working with a database, the four basic operations any must know is CRUD (Create, Read, Update, and Delete). However, we will target read, update, and delete operations in thisΒ blog.

Let’s Start from theΒ basics.

1) SELECT

If you want to know the first_name, last_name, and email of the customers, you can specify the list of the desired columns along with the SELECT and FROM keywords.

SELECT first_name,
last_name,
email
FROM customer
Image byΒ Author

Whereas, to query all the columns in the table, then asterisks(*) is the one that goes with SELECTΒ .

SELECT * 
FROM customer
Image byΒ Author

2) DISTINCT

DISTINCT restricts the duplication of rows in the query. Example: Query to print the creation date of the customers.

SELECT DISTINCT create_date
FROM customer
Image byΒ Author

3) WHERE

WHERE is used in a query to filter the result. Example: Query all the addresses in the Texas district.

SELECT * FROM address
WHERE district='Texas'
Image byΒ Author

4) GROUP BY andΒ HAVING

GROUP BY clause clusters the rows with the same values. Example: What is the total payment made by each customer?

SELECT customer_id,
SUM(amount) AS total_amount
FROM payment
GROUP BY customer_id
Image byΒ Author

In SQL, aggregation functions such as SUM, AVG, andCOUNT can not be used in the WHERE clause. We need to use the HAVINGΒ clause.

SELECT customer_id,
SUM(amount) AS total_amount
FROM payment
GROUP BY customer_id
HAVING SUM(amount) <=50
Image byΒ Author

5) ORDER BY andΒ LIMIT

ORDER BY sorts the results in ascending or descending order based on certain columns, ASC and DESC keywords decide the order of the sort. Example: Find the total payment made by each customer and sort in ascending order.

SELECT customer_id,
SUM(amount) AS total_amount
FROM payment
GROUP BY customer_id
ORDER BY total_amount ASC
Image byΒ Author

LIMIT restricts the number of rows to a specified number in the result. Example: Find the top 3 highest paying customers.

SELECT customer_id, 
SUM(amount) AS total_amount
FROM payment
GROUP BY customer_id
ORDER BY total_amount DESC
LIMIT 3
Image byΒ Author

Note: Default Order By clause sorts the result in ASCENDING order. Few SQL providers support TOP n keywords to limit the number of rows in theΒ result.

6) CASE

The CASE expression goes through conditions and returns a value when the first condition is met. Example: A customer is a premium customer if the amount exceedsΒ 50.

Image byΒ Author
SELECT customer_id,
SUM(amount) AS total_amount,
CASE
WHEN SUM(amount)>=50 THEN 'Premium Customer'
ELSE 'Standard Customer'
END AS customer_status
FROM payment
GROUP BY customer_id
ORDER BY total_amount ASC
Image byΒ Author

7) JOINS

INNER JOIN that will result in the common rows between twoΒ tables.

LEFT JOINreturns all rows from the left table and the matching rows from the right table. If no matching rows are found in the right table, NULL is used. RIGHT JOIN is a vice-versa of leftΒ join.

FULL JOIN is a combination of left and right join. If no matching rows are found in the left or right table, NULL isΒ used.

Image byΒ Author
SELECT c.first_name,
c.last_name,
c.customer_id,
SUM(p.amount)
FROM customer c
INNER JOIN payment p
ON c.customer_id = p.customer_id
GROUP BY c.first_name,
c.last_name,
c.customer_id
Image byΒ Author
SELECT c.first_name,
c.last_name,
c.customer_id,
SUM(p.amount)
FROM customer c
LEFT JOIN payment p
ON c.customer_id = p.customer_id
GROUP BY c.first_name,
c.last_name,
c.customer_id
Image byΒ Author

8) Subqueries

A subquery is a SQL query nested inside a larger query.Β Example:

SELECT payment_id,
amount,
(SELECT SUM(amount) FROM payment) AS total_amount
FROM payment
Image byΒ Author

9) Windows Function withΒ Rank

Window functions apply to aggregate and ranking functions over a particular window (set of rows). OVER clause is used with window functions to define that window. It can be combined with PARTITION BY and ORDER BYΒ .

SELECT customer_id, 
SUM(amount) AS total_amount,
RANK() OVER (ORDER BY SUM(amount) DESC)
FROM payment
GROUP BY customer_id
Image byΒ Author

10) INSERT, UPDATE, DELETE andΒ TRUNCATE

As the name suggests INSERT is used to push one or more records into the table, while UPDATEis used to modify values in the table based on certain conditions provided by WHEREΒ clause.

DELETE and TRUNCATE both are used to remove records from the table where DELETE removed the record based on WHERE condition and TRUNCATE removed all the records in anyΒ table.

Note: We have used PostgreSQL, which is an open-source Relational DBMS, pgAdmin as a Client. Queries ran on β€˜dvdrental’ database, and I have mentioned all the resources about the database and PostgreSQL in the reference section.

References:

[1] PostgreSQL Official Page. https://www.postgresql.org/

[2] pgAdmin Official Page. https://www.pgadmin.org/

[3] PostgreSQL docker Image. https://hub.docker.com/_/postgres

[4] DVD Rental DB. https://www.postgresqltutorial.com/postgresql-getting-started/load-postgresql-sample-database/


Top 10 SQL Queries a Data Scientist should know was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.

Join thousands of data leaders on the AI newsletter. It’s free, we don’t spam, and we never share your email address. Keep up to date with the latest work in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.

Published via Towards AI

Feedback ↓