SQL Statement Fundamentals: The LIMIT Statement

[av_section min_height=” min_height_px=’500px’ padding=’default’ shadow=’no-shadow’ bottom_border=’no-border-styling’ bottom_border_diagonal_color=’#333333′ bottom_border_diagonal_direction=’scroll’ bottom_border_style=’scroll’ scroll_down=” id=” color=’main_color’ custom_bg=” src=” attach=’scroll’ position=’top left’ repeat=’no-repeat’ video=” video_ratio=’16:9′ video_mobile_disabled=” overlay_enable=” overlay_opacity=’0.5′ overlay_color=” overlay_pattern=” overlay_custom_pattern=”] [av_image src=’http://www.zldoty.com/wp-content/uploads/2017/02/DSC_0265-1500×430.jpg’ attachment=’941′ attachment_size=’featured’ align=’center’ styling=” hover=” link=” target=” caption=” font_size=” appearance=” overlay_opacity=’0.4′ overlay_color=’#000000′ overlay_text_color=’#ffffff’ animation=’no-animation’][/av_image] [/av_section][av_one_full first min_height=” vertical_alignment=” space=” custom_margin=” margin=’0px’ padding=’0px’ border=” border_color=” radius=’0px’ background_color=” src=” […]

[av_section min_height=” min_height_px=’500px’ padding=’default’ shadow=’no-shadow’ bottom_border=’no-border-styling’ bottom_border_diagonal_color=’#333333′ bottom_border_diagonal_direction=’scroll’ bottom_border_style=’scroll’ scroll_down=” id=” color=’main_color’ custom_bg=” src=” attach=’scroll’ position=’top left’ repeat=’no-repeat’ video=” video_ratio=’16:9′ video_mobile_disabled=” overlay_enable=” overlay_opacity=’0.5′ overlay_color=” overlay_pattern=” overlay_custom_pattern=”]

[av_image src=’http://www.zldoty.com/wp-content/uploads/2017/02/DSC_0265-1500×430.jpg’ attachment=’941′ attachment_size=’featured’ align=’center’ styling=” hover=” link=” target=” caption=” font_size=” appearance=” overlay_opacity=’0.4′ overlay_color=’#000000′ overlay_text_color=’#ffffff’ animation=’no-animation’][/av_image]

[/av_section][av_one_full first min_height=” vertical_alignment=” space=” custom_margin=” margin=’0px’ padding=’0px’ border=” border_color=” radius=’0px’ background_color=” src=” background_position=’top left’ background_repeat=’no-repeat’ animation=” mobile_display=”]
[av_textblock size=” font_color=” color=”]
Welcome to the next installment in our journey of learning SQL. For a quick recap, here’s what we’ve learned recently in SQL:

Today, we’ll be discussing the LIMIT statement. The LIMIT statement allows you to limit the number of rows you get back after a query. The LIMIT statement is useful when you want to return all the columns in a table, but not return all hundred thousand or so rows. The LIMIT statement will typically be placed at the end of a SQL query.
[/av_textblock]

[av_heading heading=’LIMIT Statement Syntax Examples’ tag=’h2′ style=” size=” subheading_active=” subheading_size=’15’ padding=’10’ color=” custom_font=”][/av_heading]

[av_textblock size=” font_color=” color=”]
Let’s jump in!  Here’s our basic syntax example of the LIMIT statement within a SQL query:
[/av_textblock]

[av_codeblock wrapper_element=” wrapper_element_attributes=”]
SELECT column1,column2,column3 FROM table LIMIT [Number];
[/av_codeblock]

[av_textblock size=” font_color=” color=”]
Say we’ll continue using the dvd rental sample SQL database, here’s another sample query:
[/av_textblock]

[av_codeblock wrapper_element=” wrapper_element_attributes=”]
SELECT first_name,last_name,email FROM customer LIMIT 5;
[/av_codeblock]

[av_textblock size=” font_color=” color=”]
So above, we would call in the first name, last name and email columns from the customer table, and only wanting to get back the first 5 rows. Let’s also take a look at it in pgAdmin.
[/av_textblock]

[av_image src=’http://www.zldoty.com/wp-content/uploads/2017/02/2017-02-04-001-LIMIT-Statement-Example-1-300×127.png’ attachment=’939′ attachment_size=’medium’ align=’center’ styling=” hover=” link=” target=” caption=” font_size=” appearance=” overlay_opacity=’0.4′ overlay_color=’#000000′ overlay_text_color=’#ffffff’ animation=’no-animation’][/av_image]

[av_textblock size=” font_color=” color=”]
We can also combine what we’ve learned previously about SELECT DISTINCT statements. Say we want to get the first 10 unique values from a database, say the first 10 unique districts/states in our address SQL table. Take a look below.
[/av_textblock]

[av_image src=’http://www.zldoty.com/wp-content/uploads/2017/02/2017-02-04-002-LIMIT-Statement-Example-2-DISTINCT-300×211.png’ attachment=’940′ attachment_size=’medium’ align=’center’ styling=” hover=” link=” target=” caption=” font_size=” appearance=” overlay_opacity=’0.4′ overlay_color=’#000000′ overlay_text_color=’#ffffff’ animation=’no-animation’][/av_image]

[av_heading heading=’More Context About the LIMIT Statement’ tag=’h2′ style=” size=” subheading_active=” subheading_size=’15’ padding=’10’ color=” custom_font=”][/av_heading]

[av_textblock size=” font_color=” color=”]
We covered an obvious application of why we would use the LIMIT statement: you can’t always crash your server pulling down 1 million rows to just get . Duh. But we can also harness the power of SQL by combining LIMIT with other statements. For example, we can use the ORDER BY statement (in an upcoming article) to get the top X or bottom X number of results.
[/av_textblock]

[av_heading heading=’Wrap-Up’ tag=’h2′ style=” size=” subheading_active=” subheading_size=’15’ padding=’10’ color=” custom_font=”][/av_heading]

[av_textblock size=” font_color=” color=”]
This was a quick session! The real power of the LIMIT statement (and other SQL statements) will come as we combine and mash up more granular queries to power through the noise of aggregate data, down to actionable insights. If you need a refresh on previous sections, check out our How to Learn SQL section.
[/av_textblock]
[/av_one_full]

%d bloggers like this: