PGoCareerGoCareer prep tools
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

SQL · Theory

CTE

← All stacks

Theory

282/439

CTE

CTE is WITH name AS (SELECT …) SELECT … FROM name; A named subquery you can read top-down. MySQL 8+. WITH fail AS (SELECT * FROM students WHERE marks < 40) SELECT COUNT(*) FROM fail;

WITH RECURSIVE walks a tree (org chart). Don’t start there. Start with one WITH as a readable subquery.

Trap — CTE on MySQL 5.7 (not supported). Trap: recursive CTE without a stop condition.

On the example next to this theory: CTE: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

Exam tip

WITH fail AS (SELECT … WHERE marks < 40) SELECT * FROM fail;

CTE — sample query

-- CTE
CREATE TABLE demo (
  id INTEGER PRIMARY KEY,
  label TEXT NOT NULL,
  qty INTEGER DEFAULT 0
);
INSERT INTO demo (id, label, qty) VALUES
  (1, 'alpha', 2),
  (2, 'beta', 5);
SELECT label, qty FROM demo WHERE qty >= 2 ORDER BY qty DESC;

CTE: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

Short notes

  • DefWITH alias AS (query) — named subquery (MySQL 8).
  • RuleMakes long SQL readable. RECURSIVE is a later topic.
  • TrapCTE on 5.7.

Questions

1

What version added CTE?

282 / 439

P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.