IT & Technology tutorial

SQL in 15 Minutes: SELECT, WHERE, GROUP BY and JOIN

Query data with SELECT, filter with WHERE, summarize with GROUP BY and combine tables with JOIN, using two small example tables.

  • Beginner
  • 2 min read
  • Free

SQL (Structured Query Language) is how you ask questions of a database. We’ll use two tables:

employees
idnamedept_id
1Ravi10
2Maria20
3Chen10
departments
iddept_name
10Engineering
20Finance

SELECT and WHERE

SELECT name
FROM employees
WHERE dept_id = 10;
-- Ravi, Chen

ORDER BY

SELECT name FROM employees ORDER BY name;
-- Chen, Maria, Ravi

GROUP BY

SELECT dept_id, COUNT(*) AS headcount
FROM employees
GROUP BY dept_id;
-- 10 | 2
-- 20 | 1

JOIN

SELECT e.name, d.dept_name
FROM employees e
JOIN departments d ON d.id = e.dept_id;
-- Ravi  | Engineering
-- Maria | Finance
-- Chen  | Engineering
Tip: use WHERE to filter rows before grouping and HAVING to filter groups after, for example HAVING COUNT(*) > 1.

Practice

List each department name with its number of employees.
SELECT d.dept_name, COUNT(e.id) AS headcount
FROM departments d
LEFT JOIN employees e ON e.dept_id = d.id
GROUP BY d.dept_name;

Want a live class on this?

An advisor will call you within one business day to recommend a course and a batch that fits your schedule.

  1. An advisor calls you within one business day.
  2. You talk through your background and goals for about 20 minutes.
  3. You get a recommended program and batch, with no obligation to enroll.
info@stratedgeitconsulting.com +1 (302) 434-8889 ext. 101 Monday to Saturday, 9 AM to 7 PM ET

Tell us about you

Takes about a minute. Fields marked optional can be skipped.

Enter your full name.
Enter an email address like name@example.com.
Enter a phone number so an advisor can call you.
Choose what you’re interested in.
Preferred batch
Tick the box so an advisor can contact you.

We use these details as described in our Privacy Policy. By submitting, you agree to our Terms and Conditions.