Most voted "sql" questions
Structured Query Language (SQL) is a language to query databases. Questions should include examples of table code and structure. This tag refers to the default language, not to be used for questions about the implementation of specific DBMS (such as Mysql, Postgresql, Oracle, MS SQL Server). In this case use the specific DBMS tag. Answers to questions marked with ANSI SQL should use SQL as much as possible.
Learn more…6,771 questions
Sort by count of
-
6
votes3
answers14615
viewsHow to remove a Foreign key in mysql?
I need to edit a table, but I cannot remove it. I need to remove Foreign key, however, all commands I tried did not work. I’m currently trying to alter table tabela drop foreign key nomefk And the…
-
6
votes1
answer200
viewsSearch for people with similar names
I am developing code to search for people names intelligently using SQL SERVER LIKE operator. In names like Souza and Sousa just use brackets [] Ex.: select * from pessoas where nome like 'joão…
-
6
votes1
answer662
viewsSQL Server Data Dictionary
Hello, I come from Oracle culture and want to learn a little SQL Server, I’m having difficulty finding the data dictionary of SQL Server, I’ve searched in other forums and found nothing. In Oracle…
-
6
votes2
answers1345
viewsChange Boolean column to integer
I’m using a database that has a field called: status: boolean, but I want to store in it the values of the radio button (0,1,2) so I need to change to integer. I’ve tried to: ALTER TABLE OS ALTER…
-
6
votes2
answers1324
viewsReturn 0 when SUM is NULL
When you find no results you return to me NULL, how can I make the search return me 0 when it is NULL? SELECT SUM(coluna1 + coluna2 + coluna3) FROM table WHERE nome='funalo' AND MONTH(data) = 09 AND…
-
6
votes1
answer66
viewsWhy does (a < x < b) produce a result other than ((a < x) and (x < b))?
List all fields of all products whose cost plus 30% is greater than 8 and less than 10. That was my answer: SELECT * FROM `produtos` WHERE (8 < (pcusto*1.3) < 10); This is the feedback: SELECT…
-
6
votes1
answer499
viewsCOUNT(*) x COUNT(1) x COUNT(id)
I would like to understand better about the difference between these ways of using the counter: select COUNT(*) from tabela select COUNT(1) from tabela select COUNT(id) from tabela This question…
-
6
votes2
answers724
viewsmysqli_real_escape_string Prevent SQL Injection?
According to the documentation, the function: Escapes special characters in a string of characters for use in an instruction SQL, taking into account the current character set of the connection. So:…
-
6
votes1
answer400
viewsHow to return the last value of an sql string?
I need to use the last character of a string G. Like I get it with sql server? SET @VALOR = '0000050529-G' I want to take the letter G
-
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
votes3
answers839
viewshow to select zero Count
have these tables (example) Municipio (id, nome) 1, Santos 2, Sao Paulo 3, Rio de Janeiro 4, Florianopolis 5, Porto Alegre 6, Natal Cliente (id, nomecliente, cidade) 1, Joao, 1 2, Pedro, 3 3, Maria,…
-
6
votes2
answers1334
viewsSQL decimal field size
I am making a form, and in the database documentation there are some numerical fields that are divided into 2 parameters. Ex: Field: Cost Type: NUMERIC Tam.: (12,4) What does this size divide and…
-
6
votes2
answers1944
viewsView or temporary table?
Analyzing a code problem I came across different approaches to similar problems where one has a view for data access, and another uses a temporary table. I searched and found this question which…
-
6
votes1
answer419
viewsHow to rename a Constraint in mysql?
I created a Constraint primary key in my table as follows: CONSTRAINT PK_user_type PRIMARY KEY(id) But now I need to rename it: CONSTRAINT PK_type PRIMARY KEY(id) Based on this, I have the following…
-
6
votes3
answers475
views -
6
votes3
answers237
viewsQuery SQL Add duplicate value
When running the SQL query I get the following result; However, I would like to receive the result as follows; Executed SQL query; SELECT CODIGO_BARRAS.barra, produtos_eventos.quantidade FROM…
sqlasked 5 years, 11 months ago Hugo Rutemberg 307 -
6
votes1
answer62
viewsAlign queries and pass your results as alias for SOMA
So I’m running two queries that return me the amount X of items per month over a year. The first consultation gives me the following result The 'no column name' would be the months, so I have no…
sqlasked 5 years, 9 months ago Lucas Tadeu Egídio Arruda 281 -
6
votes2
answers256
viewsConcatenate columns and return the relation by SELECT
Suppose I have the following tables and their relations: Tabela A Col 1 Col 2 Col 3 Tabela B Col 1 Col 2 Col 3 Tabela C Col 1 Col 2 Col 3 Tabela D Col 1 Col 2 Col 3 Relacionamentos: A (col 1)…
-
6
votes1
answer141
viewsHow to determine a gap condition between records?
In the case of records containing date and/or time, how to determine that between the first record returned and the others there is always the same interval? Let’s take this example: Raw data:…
-
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
votes4
answers18077
viewsHow to organize auto-increment numbering of a column Id of a table in Mysql?
I would like after each insert or delete from my table to use an SQL script to rearrange the Ids. It’s kind of like this now: Id Nome 1 João 3 José 5 Ricardo I want to leave it like this: Id Nome 1…
-
5
votes1
answer243
viewsGet an image of the body of an email, write to the database or in memory and show it in a new Viewer
I’m trying to build an E-mail viewer. To do so, I access my IMAP server and get email using the DLL ComponentSoft.Net.Mail. After getting the message, I intend to present it in my viewer. However, I…
-
5
votes1
answer2375
viewsWhat is Rameter Sniffing?
I’ve heard of Parameter sniffing. Something to do with SQL Server, stored procedures, and parameters. But what exactly is?
-
5
votes3
answers593
viewsFind string in more than one SQL table
I would like to search for a certain string using LIKE in more than 1 table. Here is an example of how I did, which will probably spend a lot of time or will not search in all tables: string SQL1 =…
-
5
votes3
answers2432
viewsList months by year in mysql
Here’s what I’m trying to do php and mysql: What is in parentheses is the number of results within each month. For this, I am saving in my database, in the table tbl_noticias the field data of the…
-
5
votes2
answers541
viewsProblem with LEFT JOIN using Pentaho Kettle
There is a behavior with respect to JOIN that I cannot understand. I have two tables in these formats Table Doente ID_Doente Doente 1 Pedro 2 Paulo 3 Rui Table query ID_Consulta ID_Doente Tipo 1 2…
-
5
votes5
answers36671
viewsHow to get the total character size of an SQL column?
I want to take the total size of characters that a column supports, example INT supports 10 characters. I tried to use DATALENGTH that does not return the number of characters, but the amount of…
-
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
votes1
answer822
viewsQuery slows down with ORDER BY
I have a problem, I have a query and when I use the ORDER BY she gets too slow. Note: I created an Index for the premium field and another for the score Follow the full query: SELECT p.id AS…
-
5
votes3
answers19465
viewsConvert string to MYSQL integer
What command can I use to convert string to integer in mysql? for example: I am making the following list of tables: SELECT mov . * , prod.produto, prod.unidade, prod.icms, prod.ipi, prod.codigo…
-
5
votes3
answers5013
viewsSubtraction of multiple selects
I am learning about SQL and I have a certain problem to do certain calculations. I would like to subtract the values of selects below. I have tried to do the SUM ( A - B - C), but without success.…
-
5
votes3
answers51716
viewsSQL Server - Check duplicate data in two simultaneous fields
DBMS: SQL Server 2014; Problem: I need to know which records are duplicated, so that it is checked in two simultaneous fields, that is, if the field "CPF" with the value '83971465842' and the field…
-
5
votes3
answers18734
viewsCalculate Difference between Dates in SQL Server, with Years, Months and Days output
What would be the SQL (SQLSERVER) that could extract the difference between two dates (the largest being the current date of the system) with output of Years, Months and Days?
-
5
votes1
answer677
viewsHow do I create scheduled events in SQL Server, similar to Mysql’s "Event"?
In Mysql I used this syntax to create a scheduled event: CREATE EVENT 'evento_LimparPaper' ON SCHEDULE EVERY 1 MONTH STARTS '2014-06-3 00:00:01' ON COMPLETION NOT PRESERVE ENABLE COMMENT '' DO BEGIN…
-
5
votes1
answer167
viewsDoubt with relational modeling
I’m setting up a land registry, where these properties belong to an address, which belongs to a neighborhood that belongs to a city. Ex tables: Imoveis: id, endereco_id, etc... Enderecos: id, nome,…
-
5
votes3
answers339
viewsWhy is ':' used in queries?
I was looking at a php script and noticed that every time SQL query was written, this "operator". A part of the code $sql = "INSERT INTO categorias (nome) VALUES(:nome)"; $stmt = DB::prepare($sql);…
-
5
votes1
answer186
viewsSimulate the same SQL in the Entity Framework with Linq and Lambda?
Having this data model I want to generate a SQL thus in the Entity Framework with Linq and Lambda? Support data SELECT Pessoa.PessoaId, Pessoa.Nome, Pessoa.PessoaPaiId, PessoaPai.Nome AS NomePai,…
-
5
votes1
answer68
viewsIsolated Storage or SQL Server Compact
I’m making for Windows Phone 8.1 something similar to a dictionary where there will be a lot of text with sort, search, sort by name. Bearing in mind that there will be this large amount of…
-
5
votes2
answers1344
viewsQuote problem when doing INSERT SQL
I’m doing a CRUD with PDO, but the method query prepare() only works with "crooked quotes" Ex (that’s how it works): "INSERT INTO `_user` (`firstname`) VALUES ('blabla')" It doesn’t work anymore:…
-
5
votes5
answers15132
viewsChange NULL value in SQL SERVER
How to change the NULL value in sql server to for example 'NONE', how to fix it ? My result is coming like this: Florida United States NULL 16 Nevada United States NULL 17 Nevada United States Cell…
-
5
votes1
answer961
viewsCustom auto increment
I have to generate an auto increment of the type "000"+id, but I can not get to something concrete, my base is this below, I wanted only a light even of how to do, I do not need the answer itself.…
-
5
votes4
answers860
viewsSelect with indefinite amount of conditions
The thing is, I have a field of research, and I want to search in different places than what’s registered, like this: tenis nike shox preto. I put in research: tenis preto. If I use a description…
-
5
votes3
answers1627
viewsHow do you set zero or null in an SQL query of a date range, for those whose value does not exist?
Formulating the question was a little difficult, but I will try to be as explicit as possible using an example to put the question. There are the following data in my table: The aim is to carry out…
-
5
votes3
answers1405
viewsHow to export SQL records without duplicates?
I’m trying to remove some duplicate records from a table and I searched the Internet for how to do that, and I found something about distinct. My scenario is: I have a table that has a record with…
-
5
votes1
answer1390
viewsHow to insert into a database with special characters?
Here’s what I’m doing INSERT: $result = mysql_query("INSERT INTO tbl_livro VALUES (NULL, '$titulo', '$autor_livro')"); What happens is that there are authors who have names with special characters,…
-
5
votes2
answers814
viewsSQL function that applies to each row of the column
We work with several tables that contain a date field, sometimes we need to separate the date string into 3 columns (day/month/year). I made a function where you pass the date string and it returns…
-
5
votes2
answers9964
viewsPivot dynamic columns
I need to display this: Thus: Following the instructions of the links s: Use PIVOT operator without aggregation I cannot place a subquery inside the IN in the PIVOT Dynamic pivot with SQL Server I…
-
5
votes1
answer2172
viewsRevoke privileges
I have a database and I’m trying to make sure that only my user has access to this base. ie no one else can open, search, delete anything. I’m using the following syntax: GRANT USAGE ON batabase.*…
-
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…