📕
Dan Fitz's Notes
  • README
  • Ai
    • Supervised Machine Learning
      • Introduction To Machine Learning
      • Regression With Multiple Input Variables
      • Classification
  • Csharp
    • C Sharp Advanced
      • Generics
      • Delegates
      • Lambda Expressions
      • Events
    • C Sharp Fundamentals
      • Intro To C
      • Primitive Types And Expressions
      • Non Primitive Types
      • Control Flow
      • Arrays And Lists
      • Working With Dates
      • Working With Text
      • Working With Files
      • Debugging Applications
    • C Sharp Intermediate
      • Classes
      • Association Between Classes
      • Inheritance
      • Polymorphism
      • Interfaces
  • Java
    • Inheritance Data Structures Java
      • Inheritance Polymorphism Using Overriding And Access Modifiers
      • Abstract Classes And Debugging
      • File I O And Exceptions
      • Collections Maps And Regular Expressions
    • Intro To Java
      • Introduction To Java Classes And Eclipse
      • Unit Testing Arrays And Array Lists
      • Static Variables Methods And Polymorphism Using Overloading
  • Javascript
    • Algorithms Data Structures
      • Big O Notation
      • Analyzing Performance Of Arrays And Objects
      • Problem Solving Approach
      • Problem Solving Patterns
      • Recursion
      • Searching Algorithms
      • Bubble Selection And Insertion Sort
      • Merge Sort
      • Quick Sort
      • Radix Sort
      • Data Structures Introduction
      • Singly Linked Lists
      • Doubly Linked Lists
      • Stacks And Queues
      • Binary Search Trees
      • Tree Traversal
      • Binary Heaps
    • Complete Nodejs
      • Understanding Node.js
      • REST AP Is And Mongoose
      • API Authentication And Security
      • Node.js Module System
      • File System And Command Line Args
      • Debugging Node.js
      • Asynchronous Node.js
      • Web Servers
      • Accessing API From Browser
      • Application Deployment
      • Mongo DB And Promises
    • Complete React Native
      • Working With Content
      • Building Lists
      • Navigating Users Between Screens
      • State Management
      • Handling Screen Layout
      • Setting Up An App
      • More On Navigation
      • Advanced Statement Management With Context
      • Building A Custom Express API
      • In App Authentication
    • Epic React
      • React Fundamentals
      • React Hooks
      • Advanced React Hooks
      • Advanced React Patterns
      • React Performance
    • Fireship Firestore
      • Firestore Queries And Data Modeling Course
      • Model Relational Data In Firestore No SQL
    • Functional Light Javascript
      • Intro
      • Function Purity
      • Argument Adapters
      • Point Free
      • Closure
      • Composition
      • Immutability
      • Recursion
      • List Operations
      • Transduction
      • Data Structure Operations
      • Async
    • Js Weird Parts
      • Execution Contexts And Lexical Environments
      • Types And Operators
      • Objects And Functions
      • Object Oriented Java Script And Prototypal Inheritance
      • Defining Objects
    • Mastering Chrome Dev Tools
      • Introduction
      • Editing
      • Debugging
      • Networking
      • Auditing
      • Node.js Profiling
      • Performance Monitoring
      • Image Performance
      • Memory
    • React Complete Guide
      • What Is React
      • React Basics
      • Rendering Lists And Conditionals
      • Styling React Components
      • Debugging React Apps
      • Component Deep Dive
      • Building A React App
      • Reaching Out To The Web
      • Routing
    • React Testing
      • Intro To Jest Enzyme And TDD
      • Basic Testing
      • Redux Testing
      • Redux Thunk Testing
    • Serverless Bootcamp
      • Introduction
      • Auction Service Setup
      • Auction Service CRUD Operations
      • Auction Service Processing Auctions
    • Testing Javascript
      • Fundamentals Of Testing
      • Static Analysis Testing
      • Mocking Fundamentals
      • Configuring Jest
      • Test React Components With Jest And React Testing Library
    • Typescript Developers Guide
      • Getting Started With Type Script
      • What Is A Type System
      • Type Annotations In Action
      • Annotations With Functions And Objects
      • Mastering Typed Arrays
      • Tuples In Type Script
      • The All Important Interface
      • Building Functionality With Classes
    • Web Performance With Webpack
      • Intro
      • Code Splitting
      • Module Methods Magic Comments
  • Other
    • Algo Expert
      • Defining Data Structures And Complexity Analysis
      • Memory
      • Big O Notation
      • Logarithm
      • Arrays
      • Linked Lists
      • Hash Tables
      • Stacks And Queues
      • Strings
      • Graphs
      • Trees
    • Aws Solutions Architect
      • AWS Fundamentals IAM EC 2
    • Fundamentals Math
      • Numbers And Negative Numbers
      • Factors And Multiples
      • Fractions
    • Mysql Bootcamp
      • Overview And Installation
      • Creating Databases And Tables
      • Inserting Data
      • CRUD Commands
      • The World Of String Functions
      • Refining Our Selections
      • The Magic Of Aggregate Functions
    • Random Notes
      • Understanding React Hooks
  • Python
    • Data Analysis Using Python
      • Loading Querying And Filtering Data Using The Csv Module
      • Loading Querying Joining And Filtering Data Using Pandas
      • Summarizing And Visualizing Data
    • Intro To Python
      • Course Introduction Intro To Programming And The Python Language Variables Conditionals Jupyter Notebook And IDLE
      • Intro To Lists Loops And Functions
      • More With Lists Strings Tuples Sets And Py Charm
      • Dictionaries And Files
Powered by GitBook
On this page
  • DISTINCT
  • ORDER BY
  • LIMIT
  • LIKE
  1. Other
  2. Mysql Bootcamp

Refining Our Selections

In this section, we'll learn things like how to limit number of results you get back and how to sort those results.

DISTINCT

If our SELECT query has duplicate values, we can qualify it as SELECT DISTINCT to tell SQL to only return unique values.

SELECT DISTINCT author_fname, author_lname FROM books; -- note that multiple columns tells SQL that all columns TOGETHER must be distinct
SELECT DISTINCT CONCAT(author_fname, ' ', author_lname) AS 'name' FROM books; -- you can also do it this way!

ORDER BY

When you want to order your results, you can use ORDER BY at the end of your SELECT statements.

SELECT name FROM employees ORDER BY name; -- Sorts alphanumerically in ASCENDING (ASC) order
SELECT name FROM employees ORDER BY name desc; -- Sorts alphanumerically in DESCENDING (DESC) order
SELECT name FROM employees ORDER BY hire_date; -- NOTE: You can sort by columns that you DIDN'T select
SELECT name, employee_id, hire_date ORDER BY 2; -- NOTE: This is shorthand for the 2nd selection, employee_id

Pro tip: You can perform a multiple sort, where your query sorts by the 1st column first. Then it sorts any duplicates by the 2nd column.

-- If author_lname sorts [Frieda Harris, Dan Harris], then the second sort of author_fname will sort it as [Dan Harris, Frieda Harris]
SELECT author_fname, author_lname FROM books ORDER BY author_lname, author_fname;

LIMIT

LIMIT, when used in conjunction with ORDER BY, gives you back a subset of data.

SELECT * FROM books ORDER BY released_year DESC LIMIT 10; -- returns 10 newest books

When you want to define where your limit point starts, you write LIMIT <starting_index>,<number_of_items>.

  • Note: The starting index is zero-indexed.

SELECT * FROM books ORDER BY released_year DESC LIMIT 0,10; -- returns 10 newest books
SELECT * FROM books ORDER BY released_year DESC LIMIT 10,10; -- returns 11th-20th newest books
SELECT * FROM books ORDER BY released_year DESC LIMIT 20,10; -- returns 21st-30th newest books

Pro tip: Using LIMIT with a starting index is great for things like pagination!

Pro tip: When you want to select from a certain index all the way to the end of the table, provide a gigantic number:

LIMIT 5,99999999999999999999

LIKE

LIKE allows you to set conditions in WHERE that search for open-ended patterns rather than strict equality.

SELECT * FROM books WHERE released_year LIKE '19__'; -- returns books released in the 1900s
SELECT * FROM books WHERE author_lname LIKE 'Mc%'; -- returns books with author last names beginning with Mc
SELECT * FROM books WHERE title LIKE '%\%%'; -- returns books with % symbol somewhere in title

_ is a wildcard representing exactly 1 character. % is a wildcard representing an indefinite number of characters (including zero).

Note: LIKE is case insensitive, so in the example above, Mc% and mc% both work.

Pro tip: Any search functionality usually employs LIKE on some level.

PreviousThe World Of String FunctionsNextThe Magic Of Aggregate Functions

Last updated 3 years ago