1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| 练习使用EXISTS操作符进行嵌套查询操作。请完成以下习题: 使用EXISTS子查询在Pubs数据库titles 表及publishers表中查询New Moon Books出版社所出版的图书名称(title)
select title from titles where exists (select * from publishers where pub_name='New Moon Books')
T-SQL高级查询课堂练习及答案
USE pubs GO SELECT max(price) FROM titles where type='popular_comp' GO
USE pubs GO SELECT count(distinct type) FROM titles GO
USE pubs GO SELECT state, count(*) FROM authors group by state order by 1 GO
USE pubs GO SELECT pub_id, sum(ytd_sales) FROM titles group by pub_id order by 1 GO
USE pubs GO SELECT pub_id,avg(price) '平均价格' FROM titles GROUP BY pub_id HAVING avg(price) > 18 GO
USE pubs GO SELECT type,max(price) '平均价格' FROM titles GROUP BY type HAVING max(price) > 20 GO
找出title_id和pub_name的对应关系。 Use pubs go Select titles.title_id, publishers.pub_name From titles JOIN publishers ON titles.pub_id=publishers.pub_id Go
Use pubs go Select titles.title_id, titles.title, publishers.pub_name From titles JOIN publishers ON titles.pub_id=publishers.pub_id Go
Use pubs go Select authors.au_id, authors.au_fname + '.' + authors.au_lname 'name', titleauthor.title_id From authors JOIN titleauthor ON authors.au_id=titleauthor.au_id order by authors.au_id go
use pubs go select state,city from publishers union select state,city from authors order by 1,2
use pubs go select state,city from publishers union all select state,city from authors order by 1,2
use pubs go select title_id,au_id from titleauthor where au_id in ( select au_id from authors where state = 'CA') order by title_id go
use pubs go select t.title_id,t.au_id from titleauthor t join authors a on t.au_id = a.au_id where a.state = 'CA' order by title_id go
USE pubs GO SELECT title FROM titles WHERE EXISTS (SELECT * FROM publishers WHERE pub_id = titles.pub_id AND city LIKE 'B%') GO
USE pubs GO SELECT title FROM titles WHERE pub_id IN (SELECT pub_id FROM publishers WHERE city LIKE 'B%') GO
|