select 1 from

QUESTION POSED ON: 17 October 2002
I have a rather simple question: What does "SELECT 1 FROM ..." accomplish? I am debugging a software program that is sending a "SELECT 1" statement to the database, and I am basically curious. Does it simply select one column from the record it finds?

 

EXPERT RESPONSE

Great question. No, it selects the same column from all the rows it finds!

I would be surprised to see a "SELECT 1" query all by itself as a stand-alone query. By itself, it's useless. Like all SELECT statements, it returns a result set, and in this case, the result set consists of a number of rows, where each row has exactly one column, and where the value of that column is an integer 1. In other words, there's a 1 in each row, and nothing else! The result set has as many rows as there are in the table being selected from, subject to WHERE conditions. Hence the result set, by itself, a table of 1s, is useless. The only meaningful information you can get from it is the number of rows, and that can be obtained a lot more efficiently by using COUNT(*).

More likely, you saw it in a subselect. SELECT 1 or SELECT * or SELECT NULL are constructions commonly used in an EXISTS subselect. In an EXISTS subselect, the database does not actually "retrieve" rows, and it does not always need to scan the entire result set for the subselect, because just one row will provide an answer. That answer is either TRUE or FALSE.

There's a somewhat contrived but illustrative example of an EXISTS subselect in one of my previous answers, How does WHERE EXISTS ( SELECT NULL... ) work? (22 February 2002).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值