stamin4
  • whoami
  • HTB Academy
    • Introduction to Academy
    • Learning Process
    • Vulnerability Assessment
    • Web Requests
    • Introduction to Networking
    • Linux Fundamentals
    • Brief Intro to Hardware Attacks
    • Setting Up
    • Using the Metasploit Framework
    • Security Incident Reporting
    • Introduction to Web Applications
    • JavaScript Deobfuscation
    • Attacking Web Applications with Ffuf
    • Windows Fundamentals
    • File Inclusion
  • HTB Machines
    • Windows
      • 🟢Easy
      • 🟠Medium
      • 🔴Difficult
      • 🟣Insane
    • Linux
      • 🟢Easy
        • Cap
      • 🟠Medium
      • 🔴Difficult
      • 🟣Insane
  • OverTheWire
    • Bandit
      • Nivel 0
      • Nivel 1
      • Nivel 2
      • Nivel 3
      • Nivel 4
      • Nivel 5
      • Nivel 6
      • Nivel 7
      • Nivel 8
      • Nivel 9
      • Nivel 10
  • Base de datos
    • SQL
      • SELECT queries 101
      • Queries with constraints (Pt. 1)
      • Queries with constraints (Pt. 2)
      • Filtering and sorting Query results
      • Simple SELECT Queries
      • Multi-table queries with JOINs
      • OUTER JOINs
      • A short note on NULLs
      • Queries with expressions
      • Queries with aggregates (Pt. 1)
      • Queries with aggregates (Pt. 2)
      • Order of execution of a Query
      • Inserting rows
      • Updating rows
      • Deleting rows
      • Creating tables
      • Altering tables
      • Dropping tables
  • PortSwigger
    • Path Traversal
  • Dockerlabs
    • Trust
    • Firsthacking
    • Upload
Powered by GitBook
On this page
  1. Base de datos
  2. SQL

Filtering and sorting Query results

SQL Lesson 4

SELECT DISTINCT column(s) 
FROM mytable 
WHERE condition(s);
  • DISTINCT se utiliza para filtrar repeticiones. Es importante para optimizar.

SELECT column(s) 
FROM mytable 
WHERE condition(s) 
ORDER BY column ASC/DESC;
  • Para ordenar resultados indicamos: por qué columna queremos ordenarlos y si lo queremos de forma ascendente o descendente (funciona con números y strings, puede variar según la codificación). Si no lo indicamos, por defecto es ASC.

SELECT column(s) 
FROM mytable 
WHERE condition(s) 
ORDER BY column ASC/DESC 
LIMIT num_limit OFFSET num_offset;
  • LIMIT se utiliza para limitar el número de resultados, mientras que con el OFFSET le indicamos cuántos resultados nos queremos saltear. LIMIT funciona luego del ORDER BY.


  1. Lista todos los directores alfabéticamente, sin duplicados.

SELECT DISTINCT director 
FROM movies 
ORDER BY director;
  1. Lista las últimas cuatro películas estrenadas (de la más a la menos reciente).

SELECT * 
FROM movies 
ORDER BY year DESC 
LIMIT 4;
  1. Lista las primeras cinco películas ordenadas alfabéticamente.

SELECT * 
FROM movies 
ORDER BY title 
LIMIT 5;
  1. Lista las siguientes cinco películas ordenadas alfabéticamente.

SELECT * 
FROM movies 
ORDER BY title 
LIMIT 5 OFFSET 5;
PreviousQueries with constraints (Pt. 2)NextSimple SELECT Queries

Last updated 1 year ago