Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SQL Query CheatSheet: Part 1 Select, Order, Distinct, Limit, Count

Clause Type Query Meaning
Select all columns select * from actor; display all columns from table actor
Select specific columns select first_name,last_name from actor; display only first_name and last_name columns from table actor
Order By descending ordering select first_name,last_name from actor
order by first_name desc;
sort the selected columns by first_name in descending order Z-A
Order By ascending ordering
default
select first_name,last_name from actor
order by first_name asc;
sort the selected columns by last_name in ascending order A-Z
Order By multiple column ordering select * from payment
order by customer_id asc, amount desc;
first sorts result by customer_id in ascending – if multiple customer_id rows are present then sorts them by amount in descending
Distinct single column select distinct rating from film; distinct will give unique values in a column – will show all distinct rating values in the rating column
Distinct multiple column select distinct rating,rental_duration from film
order by rating;
distinct will apply on both columns – will return unique combinations of both columns – rating and duration
Limit select * from rental
order by rental_date desc
limit 5;
returns only top x rows – used with order by to return top x results
Count with * select count(*) from customer; Counts the number of rows in the table – customer
Count with column select count(first_name) from customer; Counts the number of rows in the column – first_name
Nulls will not be counted
Count with distinct select count(distinct first_name) from customer; Counts the number of distinct values in the column

The post SQL Query CheatSheet: Part 1 Select, Order, Distinct, Limit, Count first appeared on Helper Codes.



This post first appeared on Helper Codes, please read the originial post: here

Share the post

SQL Query CheatSheet: Part 1 Select, Order, Distinct, Limit, Count

×

Subscribe to Helper Codes

Get updates delivered right to your inbox!

Thank you for your subscription

×