Skip to content

SQL Reference

loq implements a comprehensive SQL engine for querying structured and semi-structured data.

SQL Syntax Overview

sql
SELECT [DISTINCT] columns
FROM 'filename' [alias]
[JOIN 'file2' ON condition]
[WHERE conditions]
[GROUP BY columns]
[HAVING aggregate_conditions]
[ORDER BY columns [ASC|DESC]]
[LIMIT n]

Supported Features

Basic Queries

FeatureExample
Select allSELECT * FROM file.csv
Select columnsSELECT name, age FROM file.csv
Column aliasesSELECT name AS n, age AS a FROM file.csv
ExpressionsSELECT name, age * 2 AS double_age FROM file.csv
DISTINCTSELECT DISTINCT city FROM file.csv

Filtering

FeatureExample
WHEREWHERE age > 30
AND/ORWHERE age > 30 AND city = 'NYC'
BETWEENWHERE age BETWEEN 20 AND 40
INWHERE city IN ('NYC', 'LA', 'Chicago')
LIKEWHERE name LIKE 'J%'
IS NULLWHERE email IS NOT NULL

Aggregation

FeatureExample
COUNTSELECT COUNT(*) FROM file.csv
SUM/AVGSELECT SUM(amount), AVG(amount) FROM file.csv
MIN/MAXSELECT MIN(date), MAX(date) FROM file.csv
GROUP BYSELECT city, COUNT(*) FROM file.csv GROUP BY city
HAVINGGROUP BY city HAVING COUNT(*) > 5

Sorting and Limiting

FeatureExample
ORDER BYORDER BY name ASC
Multiple columnsORDER BY city, name DESC
LIMITLIMIT 10

Advanced Features

FeatureDocumentation
JOINsJOIN Documentation
UNIONUNION Documentation
SubqueriesSubquery Documentation
Window FunctionsWindow Functions
CASE WHENCASE WHEN Documentation
CTEs (WITH clause)CTE Documentation

Quick Reference

Operators

OperatorDescriptionExample
=EqualWHERE status = 'active'
!=, <>Not equalWHERE status != 'deleted'
<, >Less/greater thanWHERE age > 21
<=, >=Less/greater or equalWHERE price <= 100
ANDLogical ANDWHERE a > 1 AND b < 5
ORLogical ORWHERE a = 1 OR b = 2
NOTLogical NOTWHERE NOT deleted
BETWEENRange checkWHERE age BETWEEN 18 AND 65
INValue in listWHERE city IN ('A', 'B')
LIKEPattern matchWHERE name LIKE '%son'
IS NULLNULL checkWHERE email IS NULL

LIKE Patterns

PatternMatches
%Any sequence of characters
_Any single character
%abcEnds with "abc"
abc%Starts with "abc"
%abc%Contains "abc"
a_c"a" + any char + "c"

Aggregate Functions

FunctionDescription
COUNT(*)Count all rows
COUNT(col)Count non-NULL values
SUM(col)Sum of values
AVG(col)Average of values
MIN(col)Minimum value
MAX(col)Maximum value
GROUP_CONCAT(col)Concatenate values

Examples by Category

Data Exploration

sql
-- Preview data
SELECT * FROM data.csv LIMIT 10

-- Count records
SELECT COUNT(*) as total FROM data.csv

-- Unique values
SELECT DISTINCT category FROM products.csv

-- Value distribution
SELECT category, COUNT(*) as count
FROM products.csv
GROUP BY category
ORDER BY count DESC

Filtering Records

sql
-- Simple filter
SELECT * FROM users.csv WHERE age >= 18

-- Multiple conditions
SELECT * FROM orders.csv
WHERE status = 'completed'
  AND total > 100
  AND created_date > '2024-01-01'

-- Pattern matching
SELECT * FROM logs.csv
WHERE message LIKE '%error%'
  OR message LIKE '%warning%'

Aggregation

sql
-- Summary statistics
SELECT
    COUNT(*) as total,
    SUM(amount) as total_amount,
    AVG(amount) as avg_amount,
    MIN(amount) as min_amount,
    MAX(amount) as max_amount
FROM sales.csv

-- Group by with filtering
SELECT department, AVG(salary) as avg_salary
FROM employees.csv
GROUP BY department
HAVING AVG(salary) > 50000
ORDER BY avg_salary DESC

Data Transformation

sql
-- String manipulation
SELECT
    UPPER(name) as name,
    LOWER(email) as email,
    CONCAT(first_name, ' ', last_name) as full_name
FROM users.csv

-- Calculations
SELECT
    product,
    price,
    quantity,
    price * quantity as total,
    ROUND(price * quantity * 0.08, 2) as tax
FROM orders.csv

-- Conditional logic
SELECT
    name,
    score,
    CASE
        WHEN score >= 90 THEN 'A'
        WHEN score >= 80 THEN 'B'
        WHEN score >= 70 THEN 'C'
        ELSE 'F'
    END as grade
FROM students.csv

All rights reserved.