Questions tagged [select]

A core SQL statement, SELECT retrieves data from one or more tables or other sources of row set data such as views or table-valued functions.

SELECT is a core SQL statement that retrieves data from various sources, typically tables, views or functions that return record set data.

Select statements can join data from multiple sources including the output of other embedded select statements (known as sub-queries). Joining is one of the key defining characteristics of a relational database management system.

a basic SELECT statement is of the form:

SELECT [column list]
  FROM [sources]
 WHERE [conditions]

A more complex query can be composed of a variety of primitive operations, including:

Joins take two data sets (tables, subquery results or other sources) and link them together based on some logical function such as equality of the columns. Some join predicates can use indexes and allow optimisations in the query that may not be possible with others. In some cases it may be necessary to re-structure the query so it can (for example) make use of an index, even though the original query was semantically equivalent to the modified version. A basic join query has the form:

SELECT [column list]
  FROM [table 1]
  JOIN [table 2]
    ON [some predicate such as equality of two columns]

A join may be optional on one or both sides, called an outer join. If data on the optional side of the join is not present, then NULLs will be returned in the place of columnar data from the data set on that side of the join.

Nested Subqueries are SQL statements embedded within a query that can be used by outer queries by selecting from that data set or joining against another. A query that joins something against the output of a nested subquery looks something like:

SELECT a.[columns]
      ,b.[columns]
  FROM [table1] a
  JOIN (SELECT [columns]
          FROM [ . . . ]) b
    ON [join condition]

Correlated Subqueries are sub-queries that use some data from the parent query. A correlated subquery might look something like:

SELECT a.[columns]
  FROM [table1] a
 WHERE EXISTS
       (SELECT 1
          FROM [table2] b
         WHERE [some condition involving data from both a and b])

In this case, note that the condition involves data from both a and b. The defining feature of a correlated subquery is that it needs data from the parent to resolve the predicate.

WHERE Clause This allows arbitrary logical conditions to be applied. In some cases these can be applied in joins as well, and it may be preferable to explicitly apply them in the join condition so the query optimiser can use them properly.

Common Table Expressions (CTEs) are an abstraction mechanism that can be used to re-use common logic in a query, or for certain facilities such as recursion. A CTE may also take parameters, and has a form similar to:

WITH [name of cte] (parameters) AS
     (SELECT [columns]
        FROM [sources]
       WHERE [predicates])  -- This could be arbitrarily complex
SELECT [. . .]

Aggreates and filters A query can aggregate data within groupings using the GROUP BY clause, and can filter after the aggregate using HAVING.

Various other features may be present in certain dialects, for example windowing functinons, Oracle's CONNECT BY, applying table valued functions (CROSS APPLY) or using table valued functions as data sources.

1082 questions
111
votes
3 answers

What is the default order of records for a SELECT statement in MySQL?

Suppose you have the following table and data: create table t ( k int, v int, index k(k) ) engine=memory; insert into t (k, v) values (10, 1), (10, 2), (10, 3); When issuing select * from t where k = 10 with no order…
daisy
  • 1,338
  • 3
  • 11
  • 17
65
votes
2 answers

Reference column alias in same SELECT list

I'm converting an old MS-Access-based system to PostgreSQL. In Access, fields that were made up in SELECTs could be used as parts of equations for later fields, like this: SELECT samples.id, samples.wet_weight / samples.dry_weight - 1 AS…
wizpig64
  • 753
  • 1
  • 5
  • 5
55
votes
5 answers

How can I use a default value in a Select query in PostgreSQL?

I would like to use a default value for a column that should be used if no rows is returned. Is that possible in PostgreSQL? How can I do it? Or is there any other way I can solve this? E.g. something like this: SELECT MAX(post_id) AS max_id DEFAULT…
Jonas
  • 33,945
  • 27
  • 62
  • 64
50
votes
8 answers

Is it possible to mysqldump a subset of a database required to reproduce a query?

Background I would like to provide the subset of my database required to reproduce a select query. My goal is to make my computational workflow reproducible (as in reproducible research). Question Is there a way that I can incorporate this select…
David LeBauer
  • 3,162
  • 8
  • 32
  • 34
46
votes
5 answers

How to SELECT from SHOW TABLE STATUS results

I'd like to limit the rows and columns that come back from the SHOW TABLE STATUS command in MySQL 5.1. Is there a way to get this same information through a SELECT statement so I can manipulate the results in a normal way?
Leopd
  • 825
  • 1
  • 7
  • 10
46
votes
7 answers

How to cast an integer to a boolean in a MySQL SELECT clause?

I'm new here so be kind to me. I have the following scenario: I have many tables which, for the sake of simplicity, are represented in a View in my MySQL database. My problem is that I need a value in this view representing if it is one kind of…
Bruno
  • 563
  • 1
  • 4
  • 5
45
votes
5 answers

Benefits of using backtick (`) in MySQL queries?

In MySQL we can create queries with or without the backtick (`) symbol. Example: SELECT * FROM TEST; SELECT * FROM `TEST`; Both works fine in mysql-console. Is there any technical difference between them? Is there any benefit using (`) over over…
Satish Pandey
  • 683
  • 1
  • 7
  • 13
41
votes
8 answers

Is select * still a big no-no on SQL Server 2012?

Back in the days of yesteryear, it was considered a big no-no to do select * from table or select count(*) from table because of the performance hit. Is this still the case in later versions of SQL Server (I'm using 2012, but I guess the question…
Piers Karsenbarg
  • 959
  • 2
  • 12
  • 23
41
votes
2 answers

Using SELECT in the WHERE clause of another SELECT

I have made a draft remote application on top of libpq for PostrgreSQL. It behaves well, but I have profiled the general functioning of the application. For each final business result that I produce, it happens that I call something like 40 select…
Stephane Rolland
  • 8,911
  • 11
  • 33
  • 40
40
votes
1 answer

USING construct in JOIN clause can introduce optimization barriers in certain cases?

It was brought to my attention that the USING construct (instead of ON) in the FROM clause of SELECT queries might introduce optimization barriers in certain cases. I mean this key word: SELECT * FROM a JOIN b USING (a_id) Just in more complex…
Erwin Brandstetter
  • 185,527
  • 28
  • 463
  • 633
35
votes
1 answer

MySQL Count rows from another table for each record in table

SELECT student.StudentID, student.`Name`, COUNT(attendance.AttendanceID) AS Total FROM student LEFT JOIN attendance ON student.StudentID = attendance.StudentID I am trying to count the last row but instead it counts all the results and…
Ali Shaikh
  • 479
  • 1
  • 6
  • 11
35
votes
4 answers

How to JOIN two table to get missing rows in the second table

In a simple voting system as CREATE TABLE elections ( election_id int(11) NOT NULL AUTO_INCREMENT, title varchar(255), CREATE TABLE votes ( election_id int(11), user_id int(11), FOREIGN KEYs for getting the list of elections a user has voted, the…
Googlebot
  • 4,551
  • 26
  • 70
  • 96
35
votes
2 answers

oracle - list users with access to certain tables

I'm sure this has been asked before but I can't seem to find the relevant details for the following. Is there some sort of pre-built table that can do the following (I have used dba_tab_privs but it is limited and does not meet all my needs), if not…
dgf
  • 369
  • 1
  • 3
  • 4
35
votes
3 answers

Why does this query work?

I have two tables, table_a (id, name) and table_b (id), let's say on Oracle 12c. Why does this query not return an exception? select * from table_a where name in (select name from table_b); From what I understand, Oracle sees this as select * from…
eagerMoose
  • 521
  • 1
  • 5
  • 11
30
votes
2 answers

Why would SELECT * be magnitudes faster than SELECT foo?

Consider a table of values and hashes, like so: +------------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+----------+------+-----+---------+----------------+ |…
dotancohen
  • 1,106
  • 6
  • 16
  • 27
1
2 3
72 73