Most voted "query" questions
Query or *query* refers to the act of recovering data previously stored in a computerised system from certain parameters and/or filters, by means of a specific command or operator.
Learn more…1,160 questions
Sort by count of
-
30
votes5
answers7016
viewsHow to delete all duplicates except one?
I have a table that has duplicate records. They shouldn’t be duplicated. Some records duplicated two, five and even thirty times by accident. I need to remove these duplicate records, but I need to…
-
28
votes7
answers33990
viewsHow do SELECT all fields except a few?
It is known (and has been asked) which should be avoided SELECT * in some cases in bank queries. But let’s imagine that I have a table with 50 columns, and I want to select 40 of them. Just the fact…
-
17
votes1
answer569
viewsWhat is Sargable argument?
In database queries what is the concept of a sargable argument (Search Argument Able)?
-
17
votes3
answers1699
viewsWhy doesn’t "= NULL" work?
In the SQL Server, by fetching all records with a given field with value NULL, if I do so no record is returned: SELECT * FROM clientes cli WHERE cli.cpf = NULL But if you use the following syntax:…
-
14
votes4
answers2496
viewsPerformance of COUNT(*) and COUNT(1)
What’s the difference between COUNT(1) and COUNT(*) in an SQL query. For example: SELECT COUNT(1) FROM USUARIOS; and SELECT COUNT(*) FROM USUARIOS; Is there any difference of interactions within…
-
14
votes3
answers2619
viewsCan subqueries decrease performance? Myth or truth?
Well, I usually just work with frameworks. I work with frameworks MVC and I usually use Orms to query the database. In my case I use Laravel, but I’ve used other frameworks, and had the opportunity…
-
12
votes2
answers312
viewsOptimization of SQL code
How can I optimize the following code to not use 3 Selects and not plaster the query to only 3 status ( SELECT * FROM historico WHERE his_status = 'FRACASSO' ORDER BY his_data DESC LIMIT 50 ) UNION…
-
11
votes1
answer3203
viewsLeft Join or Not Exists
I needed to retrieve information that was in a certain table, but were not in another one. Searching found that NOT EXISTS would serve this case, but I saw that Left Join brings the same result: NOT…
-
11
votes1
answer8640
viewsLimit and Offset for Web Paging in SQL Server
I am working with PHP and have about 8000 records currently on DB. As it is for a web page, I need to make a pagination to be able to read the data. From what I understand, I have to do something…
-
10
votes1
answer12629
viewsWhy should we use "IS NOT NULL" instead of "<> NULL"?
I’ve always wondered why we should use IS NOT NULL instead of <> NULL? For when I do as in the second case, no result is found. Example: SELECT * FROM tabela WHERE campo IS NOT NULL Displays…
-
10
votes2
answers341
viewsError updating the entire JAVA table
I’m trying to perform a mass update to update all prices of products registered in the system, adding the value passed by the user to the unit price of the registered product, according to the…
-
9
votes2
answers33949
viewsHow to catch the current year on ORACLE?
I need to get the annual year on ORACLE, but I only know how to get the current date, so: SELECT SYSDATE FROM DUAL You can manipulate it to get only the Year?
-
9
votes2
answers1340
viewsSmart Query with Mysql
I’m trying to make a "smarter" query on my DB, my doubt is as? I have a query: SELECT * from publicacao where titulo like '%$busca%'; and in my DB has several publishing titles, for example:…
-
9
votes3
answers3392
views_ (underline) in consultation with LIKE
I’m making a query where I need to return all information from a given table, which contains "_R_" in the nomenclature. However, when using WHERE NM_CAMPAIGN LIKE '%_R_%' he returns me to other…
-
8
votes1
answer1608
viewsHow to apply the IF ELSE condition to selecting a column?
In Mysql, I have an old table entities where the column gender is in the format ENUM, containing values of the type: "i", "m", "f". When migrating this data to the new structure of this table, I…
-
8
votes1
answer50072
viewsHow to get in SQL the last record inserted according to its date
I need to get a record from a table, where this record is what in date terms was last entered. Gender: SELECT Ped.Quantidade FROM ped WHERE data <= @data But since I have several records it…
-
8
votes2
answers654
viewsPerformance of query LIKE in Mysql
Is there any way to increase the performance of a query with LIKE '%string%' in Mysql? I know that if the LIKE for 'string%', is faster. The problem is when the % is at the beginning of the string.…
-
8
votes1
answer81
viewsProblems with a MYSQL query - Categories and Items
I’m doing a project, which basically consists of item and category... or better "animes" and "episodes"... Here’s the problem, I want to list all the registered anime and a count of the episodes…
-
8
votes1
answer89
viewsHow do I extract values from an Expression?
Hello. It is possible to extract from an Expression which filters, arrays, etc were used in a query? Example: public class Program { class Produto { public int Id; public string Nome; public…
-
7
votes2
answers1493
views -
7
votes2
answers686
viewsFull Join N-N without repeating records
I have two tables with relation N-N: ITEM_ORC ITEM_COMP | OP | Item Orçado | | OP | NF | Item Comprado | +-------+------------------+ +-------+---------+------------------+ | 1 | Lápis | | 1 | 101 |…
-
7
votes1
answer451
views -
7
votes3
answers3104
viewsWhy does GROUP BY not work with Mysql in this case?
I am using the Mysql language and am trying to group a table of professions with the GROUP BY through the consultation below. SELECT name, occupation FROM OCCUPATIONS GROUP BY occupation; But I get…
-
7
votes1
answer94
viewsSQL Server JOINS vs Subquerys
I was making some queries, and came the need to carry out the grouping of a table that was in a JOIN within the query. To illustrate. Sales chart. +----+---------+------------+----------+ | Id |…
-
6
votes4
answers41617
viewsSign of different Query
I have a problem presenting the Query. I got the field alvaraValidade which only receives Data fields. When I put nothing in that field, the field date is guarded as 0000-00-00. $sql = "Select *…
-
6
votes1
answer1248
viewsSQL Query count
I need to make a query in which the result is the medals (gold, silver, bronze and total (sum of 3)) obtained by each country in all editions of the Olympic Games. So far so good, the query below…
-
6
votes1
answer616
viewsQuery to join one column from another
I have tried unsuccessfully to get a result. I have the table with the following login and logout information: |Group |User |EventDate |Event |Camp ------------------------ |Evolution |1012…
-
6
votes2
answers1238
viewsWhat is a "0" field in the SQL SELECT?
I didn’t understand the use of 0 in this code: SELECT substring ... 0 AS PARCELADO, ... FROM ... What does he do?
-
6
votes3
answers502
viewsINSERT query does not work inside a loop
So guys, I’m not getting to solve this query problem within a foreach loop not run. At first I thought it was something wrong with Pdo->beginTransaction so I commented on that part and left only…
-
6
votes2
answers6106
viewsMysql group_concat() in Sqlserver
I created a table for example: I have the following content: select * from table_t; And I need to get the following result: In Mysql I use the following query: select group_concat(concat_ws(' -…
-
6
votes3
answers71632
viewsHow to make a select in the bank not to bring repeated values?
For example have in the table the values: green, red, blue, blue, pink, pink, pink, yellow. I would need to return from the colors table only once the blue and the pink. Is there any way to do this?…
-
6
votes4
answers34297
viewsTake the highest value per category by also loading the ID
The table is in the format below ID | CATEGORIA | QUANTIDADE 1 | A | 23 2 | A | 12 3 | A | 11 1 | B | 15 2 | B | 10 Wanted a query to return to me the following table ID | CATEGORIA | QUANTIDADE 1 |…
-
6
votes3
answers324
views"Translate" query from Mysql to SQL Server 2012
What is the SQL Server 2012 query that corresponds to the next Mysql query? REPLACE INTO schedule SET jobname = "sqldump" , last_exec_date = NOW()…
-
6
votes4
answers5546
viewsSQL query to get all records from last month but until the current day
Good morning, I need to know a percent variation of last month’s job records for the task logs you did this month. For this I need to compare with the current day, for example. If today were…
-
6
votes3
answers688
viewsHow to do in a select html, when clicking select more than one row at a time?
I have a query that brings me the first 3 letters of a month name with the last two digits of the year for example: JAN.16 in my combobox it brings me the independent months for example: JAN.16…
-
6
votes2
answers1510
viewsHow to view the last 30 days of a query
I’m trying some alternatives to show only the records of the last 30 days, but I couldn’t make it work, the query I have is like this: SELECT comunidade.descricao AS nomeunidade ,comcargo.descricao…
-
6
votes1
answer1546
viewsHow to round up time?
I have the following function which treats an hour coming from the bank: SELECT f.chapa AS chapa, f.nome AS nome, f.secao AS cod_secao, f.nsecao AS desc_secao, c.codigo AS cod_funcao, c.nome AS…
-
6
votes0
answers4119
viewsShortcut to Identar Query Sql
Good afternoon, everyone! Is there any keyboard shortcut that makes the Query SQL identation and leaves the code organized? In the Database.Net application the command is Ctrl+M If anyone knows…
-
6
votes2
answers1019
viewsselect bringing all un-duplicated records based on a single column
How can I bring all records from a column that are not repeated. For example: | tela | url | perfil | dataCriacao | --------------------------------------------- | dica | /dica/ | ROLE_CDL |…
-
6
votes2
answers48
viewsHow to create a View with 2 tables with different columns in SQL?
I have the following query: CREATE OR REPLACE FORCE VIEW "Resumo_vendas" ( "TIPO_REGISTRO" ,"VLR_DESCONTOS" ,"VLR_ACRESCIMOS" ,"VLR_PIS" ,"VLR_COFINS" ,"VLR_ICMS" ,"DTA_EMISSAO_NOTA" ) AS ( SELECT 1…
-
5
votes1
answer114
viewsHow to select partially distinct lines?
I have a table whose column reference contains constructed values as follows:: Reference / Color / Size / Gender Examples: JOHN/WHITE/52/MALE JOHN/WHITE/51/FEMALE JOHN/BLACK/52/MALE…
-
5
votes3
answers1796
viewsQuery in an SQL string in C#
I’m doing a query in the database via string sql.append, in the method call I have two parameters, two strings these receive one textbox typed by the user and check in the database. How do I…
-
5
votes2
answers1679
viewsSearch System - Select within another
Hi, I’m running a search engine, but I’m kind of a layman when it comes to querys. The method I thought to refine the search, which in this case would be a simple search between two fields of a…
-
5
votes3
answers1785
viewsHow to deal with multiple queries?
I have a question related to good programming practices. How to deal with multiple queries on the same page in PHP. For example, the code: $qr = "SELECT historico.*, funcionarios.nome FROM historico…
-
5
votes4
answers1841
viewsDoes the order of the WHERE clauses interfere with performance?
I recently ran some tests on a database with a query using two Where AND clauses. I noticed that there was a significant difference using clause A before clause B and vice versa. Intuitively, it…
-
5
votes2
answers1325
viewsBrowse query records with PHP PDO
I’ve always worked with ASP connections using ADODB and navigating the regressions that the query returned to me was not a problem because there were: <% rs.movenext //anda para proxima linha do…
-
5
votes3
answers160
viewsDo not fill in field if it does not exist
I have a question on how to ask a question to the database. Suppose I have two tables with slightly different structures Table1 id tipoA data 0 A '21/12/14' Table2 id tipoB data 1 B '24/12/14'…
-
5
votes1
answer625
viewsHow to update fields with fields from another table?
I’m not able to update one table with another’s select. I have a table mdl_user which contains several columns and created another users_sg to copy some columns. I copied with INSERT INTO users_sg…
-
5
votes6
answers32941
viewsConvert varchar to date in SQL
You can convert dates into sweep for date. I’m having difficulties in performing searches between dates, due to this discrepancy in the database where I perform the query. When I enter my line of…
-
5
votes3
answers1988
viewsHow many days to a date
Does anyone have a query in sql server that returns how many days to a date? I have the employee’s admission date, I need to know, how many days are left to complete 45 days and how many days to…